c# winform 子父窗体交互信息

第一种利用委托的方法:

父窗体的方法:

     private void constraintEnd_Click(object sender, EventArgs e)
        {
            //if (MessageBox.Show("即将强制将本炉次数据归档!", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)//2012.4.11修改
            //{
            CCMEnd ccmend = new CCMEnd(curHeatInfo.Rows[0]["heatid"].ToString(), label337.Text.ToString().Trim());
            ccmend.resultEvent += new CCMEnd.CalculateDelegate(Endccm);
            ccmend.Show(this);
        }

        public void Endccm(string result)
        {
            checkProcStatus();
            if (result.Equals("1"))
            {
                //MessageBox.Show("归档成功!", "警告");//2012.4.11修改

                MessageBoxTimeout.Show("归档成功!", "警告", 4000);
            }
            else
            {
                //MessageBox.Show(result, "警告");
                MessageBoxTimeout.Show(result, "警告", 4000);
            }
            //}
        }
子窗体方法:
 public delegate void  CalculateDelegate(string result);

        public event CalculateDelegate resultEvent;

        private void button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox4.Text) && !string.IsNullOrEmpty(textBox5.Text))
            {
                QT_Service.QT_Service qtService = new UI.QT_Service.QT_Service();
                int cut_head = Int16.Parse(textBox4.Text);
                int cut_trimmed = Int16.Parse(textBox5.Text);
                result = qtService.EndEAFHeatID(4, string.Format("{0:yyyyMMddHHmmss}", DateTime.Now), cut_head, cut_trimmed);
                //CalculateDelegate calculate = new CalculateDelegate(((CCMMonitor)this.Owner).Endccm);
                //calculate(result);
                this.Close();
                resultEvent(result);
            }
            else
            {
                MessageBox.Show("输入有误,请重新输入!", "警告");
            }
        }
第二种方法:

父窗口:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(this,textBox1.Text);//带参数构造Form2(此方法该处带俩参数构造多余)
       
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text += frm.ReturnText;//通过Form2的属性获得回传值
            }
            frm = null;
            
        }
子窗口:

   public partial class Form2 : Form
    {
        private Form1 pfrm;
        private string frm1txt;
        public Form2(Form1 parentfrm,string frm1text)
        {
            InitializeComponent();
            pfrm = parentfrm;
            frm1txt = frm1text;
        }
      public string ReturnText//回传值的属性
        {
            get { return this.frm1txt; }
        }
          private void button2_Click(object sender, EventArgs e)//给属性赋值并关闭模式窗体Form2
        {
            frm1txt = this.textBox2.Text;
            this.DialogResult = DialogResult.OK;//*必加 模式窗体打开后 只有DialogResult初始化后才可继续操作 否则值无法传回
            this.Close();//模式窗体关闭.close()相当于将窗体隐藏 彻底关闭需要用.Dispose()
        }

        private void button3_Click(object sender, EventArgs e)//关闭
        {
            this.DialogResult = DialogResult.Cancel;//同*
            this.Close();
        } 

第三种方法就是将父窗口的对象当做参数传到子窗口。


参考: http://www.cnblogs.com/wangchunming/archive/2011/12/10/2282962.html



你可能感兴趣的:(c# winform 子父窗体交互信息)