我现在有两个类,一个有一个按钮的事件处理程序,我需要能够监听另一个类中的事件处理程序,以便在非按钮类中进行更改。我对这种类型的场景没有太多的经验,所以不太确定从哪里开始。
发布于 2013-11-08 07:05:31
下面是这两个类的一个例子(如果我没理解错的话)。
class Form
{
Button _button1, _button2;
public Form()
{
_button1 = new Button("button1");
_button2 = new Button("button2");
_button1.Click += _button_Click;
_button2.Click += _button_Click;
}
void _button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
Console.WriteLine(button.Name);
}
public void Click1()
{
_button1.FireEvent();
}
public void Click2()
{
_button2.FireEvent();
}
}
class Button
{
public event EventHandler Click;
public string Name;
public Button(string name)
{
Name = name;
}
public void FireEvent()
{
Click(this, new EventArgs());
}
}用法:
Form f = new Form();
f.Click1();
f.Click2();https://stackoverflow.com/questions/19847915
复制相似问题