日期:2014-05-18 浏览次数:20935 次
Form1窗体,添加一个button1跟一个textBox1
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 InputDialog = new Form2(this);
            InputDialog.ShowDialog(this);
        }
        public void refresh()
        {
            textBox1.Text = "success";
            MessageBox.Show("成功在关闭子窗体时更新了父窗体内容!");
        }
    }
}
Form2构造窗体,设置FormClosing就好了,代码如下:
 程序代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public Form1 Gz_fm;
        public Form2(Form1 t_Form1)
        {
            InitializeComponent();
            Gz_fm = t_Form1;
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Gz_fm.refresh();
        }
    }
}
------解决方案--------------------
主窗体 Form1,子窗体 Form2
Form2的构造中增加带参数构造
Form1 _frm;
public Form2(Form1 frm)
{
_frm = frm;
}
后面就可以通过_frm去控制Form1了
------解决方案--------------------
使用委托:
// 主窗体中
 FromB frm = new FromB();
 frm.onReportProgress = new DoReportProgress(OnReportProgress);
 frm.ShowDialog(); // 显示窗体
 private void OnReportProgress()
 {
  // 更新DataGridView
  }
// 子窗体
public delegate void DoReportProgress(int current, string strInfor);
public DoReportProgress onReportProgress;
public void button1()
{ 
 if(onReportProgress()!=null)
  onReportProgress(); // 调用委托更新父窗体
}