一、主窗口设计事件
public partial class FormMain : Form
{
public string strA;
public void FunA(string strTest)
{
strA = strTest;
}
internal void FormMainEventChanged(object sender, EventArgs e)
{
//取到主窗体的传来的文本
MyEventArg arg = e as MyEventArg;
this.FunA(arg.Text);
}
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
Global.GetInstance().CurMain = this;
}
private void button1_Click(object sender, EventArgs e)
{
FormB f = new FormB();
f.ShowDialog();
}
}
二、窗体B加载时注册事件,点击是调用
public partial class FormB : Form
{
public FormB()
{
InitializeComponent();
}
public event EventHandler SendMsgEvent; //使用默认的事件处理委托
private void FormB_Load(object sender, EventArgs e)
{
SendMsgEvent += Global.GetInstance().CurMain.FormMainEventChanged;//为主窗体注册事件,在主窗体中事件处理代码中设置文本
}
private void button1_Click(object sender, EventArgs e)
{
SendMsgEvent(this, new MyEventArg() { Text = "Test" });
}
}
三、定义一个类继承EventArgs
public class MyEventArg :EventArgs
{
//传递主窗体的数据信息
public string Text { get; set; }
}