我如何附上相同的事件处理程序中的WinForms额外的控制/ .NET / C#?
How do i attach the same event handler to additional controls in Winforms/.NET/C#?
我的任意的试图完全合乎逻辑的code来完成我想要的,但遗憾的是语法无效在C#:
i randomly tried perfectly logical code to accomplish what i want, but unfortunately the syntax is not valid in C#:
public MainForm()
{
InitializeComponent();
FixPanelMouseEnter(pnlActionCenter);
FixPanelMouseEnter(pnlAdministrativeTools);
FixPanelMouseEnter(pnlAutoPlay);
FixPanelMouseEnter(pnlBackupAndRestore);
//...snip 49 lines...
FixPanelMouseEnter(pnlWFirewall);
FixPanelMouseEnter(pnlWLiveLanguageSettings);
FixPanelMouseEnter(pnlWUpdate);
}
private void FixPanelMouseEnter(Panel panel)
{
foreach (Control ctrl in panel.Controls)
ctrl.MouseEnter += panel.MouseEnter;
}
本的无效的code使在的语法错误的
This invalid code causes the syntax error:
事件System.Windows.Forms.MouseEnter只能出现在左侧A + =或 - =
The event 'System.Windows.Forms.MouseEnter' can only appear on the left hand side of a += or -=
在这个例子中,我想小组的MouseEnter
触发事件,如果鼠标输入是在面板中的任何控制权。
In this example i want the Panel's MouseEnter
event to fire if the mouse enter's any control in the panel.
How做我附上相同的事件处理程序中的WinForms额外的控制/ .NET / C#?
在code我想不会编译。
The code i tried doesn't compile.
WinForms:如何引起的MouseEnter火灾当鼠标进入控制? .NET:如何检查鼠标是否在控制? WinForms: How to cause MouseEnter to fire when the mouse enters a control? .NET: How to check if the mouse is in a control?修改
ctrl.MouseEnter += panel.MouseEnter;
到
ctrl.MouseEnter += panel_MouseEnter;
假设方法无效panel_MouseEnter
已经存在于你的code。
Assuming the method void panel_MouseEnter
already exists in your code.
我认为你需要再通过事件处理程序,太:
I think you need to then pass the EventHandler, too:
private void FixPanelMouseEnter(Panel panel, EventHandler enterMethod) {
foreach (Control ctrl in panel.Controls)
ctrl.MouseEnter += enterMethod;
}
,然后从code:
and then from your code:
FixPanelMouseEnter(pnlActionCenter, pnlActionCenter_MouseEnter);
但同样,pnlActionCenter_MouseEnter必须已经存在。有意义吗?
But again, the pnlActionCenter_MouseEnter must already exist. Make sense?