在 Visual Basic 中,我知道该怎么做,但我是 C# 新手,所以你们能告诉我如何使用鼠标悬停创建一个“私有 void”,将相同的事件应用于多个控件吗?有一个例子:
private void button1, button2, button3, button4_MouseHover(object sender, EventArgs e)
{
btn.Image = pic
}
只需声明一个事件处理程序并将每个按钮指向它:
private void Common_MouseHover(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn != null)
btn.Image = pic
}
然后在代码或设计器中:
button1.MouseHover += Common_MouseHover;
button2.MouseHover += Common_MouseHover;
// .. etc
当您订阅按钮上的事件时,它只是一个标准事件处理程序:
button1.Click += myEventHandler;
您可以使用相同的代码为每个按钮添加处理程序:
button1.Click += myEventHandler;
button2.Click += myEventHandler;
button3.Click += myEventHandler;
button4.Click += myEventHandler;
button5.Click += myEventHandler;
button6.Click += myEventHandler;
button1.MouseOver += OnMouseOver(...)
button2.MouseOver += OnMouseOver(...)
button3.MouseOver += OnMouseOver(...)
button4.MouseOver += OnMouseOver(...)