在两个窗体AB之间,B为了调用A窗体中的方法,可以通过以下事件的方法来解决。当然实现的方法很多种
A窗体
using System;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ShowM0(object sender, EventArgs e)
{
MessageBox.Show("第0个呼叫");
}
public void ShowM1()
{
MessageBox.Show("第1个呼叫");
}
public void ShowM2(string s)
{
MessageBox.Show("传递过来的参数:" + s);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.CallM0 += new EventHandler(ShowM0);
frm2.CallM1 += new Form2.EventHandler1(ShowM1);
frm2.CallM2 += new Form2.EventHandler2(ShowM2);
frm2.ShowDialog(this);
}
}
}
B窗体
using System;
using System.Windows.Forms;
namespace test
{
public partial class Form2 : Form
{
//Eventhandler 就是事件不带委托参数的。举例
//public Event Eventhandler ClickEvent;
//其实就等同于声明
//public delegate void Eventhandler();
//public Event Eventhandler ClickEvent;
public event EventHandler CallM0;
public delegate void EventHandler1();
public event EventHandler1 CallM1;
public delegate void EventHandler2(string s);
public event EventHandler2 CallM2;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CallM0(sender, e);
CallM1();
CallM2("第2个");
}
}
}