关于C#中父子窗体间交互问题

为实现父子窗体通信,我们可以用托管来实现

关于C#中父子窗体间交互问题_第1张图片

在子窗体中定义这两个委托

        public delegate void OverLapHandler(object sender, EventArgs e);
        public event OverLapHandler OverLap;


        public delegate void NewPath(object sender, EventArgs e);
        public event NewPath newPath;


        private void button_overlap_Click_1(object sender, EventArgs e)
        {
            OverLap(sender, e);
            this.Close();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            newPath(sender, e);
            this.Close();
        }

在父窗体中定义

CheckForm checkForm = new CheckForm();
        checkForm.OverLap += new CheckForm.OverLapHandler(this.LeftChildButtonClick);
        checkForm.newPath += new CheckForm.NewPath(this.RightChildButtonClick);
        checkForm.ShowDialog();

其中LeftChildButtonClick、RightChildButtonClick就是子窗体委托父窗体完成的工作

其原型为: public void LeftChildButtonClick(object sender, EventArgs e)

   public void RightChildButtonClick(object sender, EventArgs e)

如果子窗体和父窗体之间还有信息窗体,我们就要定义事件了

父窗体:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
  
namespace FormDataInteractive
{
    /// 
    ///  powered by shadu {at} foxmail.com
    /// 
    publicpartial class Form1 : Form
    {
        publicForm1()
        {
            InitializeComponent();
        }
  
        privatevoid button1_Click(objectsender, EventArgs e)
        {
            // 创建Form2,并添加事件处理函数
            Form2 frm = newForm2();
            frm.DataChange += newForm2.DataChangeHandler(DataChanged);
            frm.ShowDialog();
        }
  
        publicvoid DataChanged(objectsender, DataChangeEventArgs args)
        {
            // 更新窗体控件
            textBox1.Text = args.name;
            textBox2.Text = args.pass;
        }
    }
}


子窗体:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace FormDataInteractive
{
    publicpartial class Form2 : Form
    {
        // 定义委托
        // public delegate void DataChangeHandler(string x); 一次可以传递一个string
        publicdelegate void DataChangeHandler(objectsender, DataChangeEventArgs args);
        // 声明事件
        publicevent DataChangeHandler DataChange;
 
        // 调用事件函数
        publicvoid OnDataChange(objectsender, DataChangeEventArgs args)
        {
            if(DataChange != null)
            {
                DataChange(this, args);
            }
        }
 
        publicForm2()
        {
            InitializeComponent();
        }
 
        privatevoid button1_Click(objectsender, EventArgs e)
        {
            // 触发事件, 传递自定义参数
            OnDataChange(this,newDataChangeEventArgs(textBox1.Text, textBox2.Text));
        }
    }
 
    /// 
    /// 自定义事件参数类型,根据需要可设定多种参数便于传递
    /// 
    publicclass DataChangeEventArgs : EventArgs
    {
        publicstring name { get;set; }
        publicstring pass { get;set; }
        publicDataChangeEventArgs(strings1, strings2)
        {
            name = s1;
            pass = s2;
        }
    }
}



你可能感兴趣的:(C#,c#,delegate,控件,通信)