我有一个包含子控件的 Panel
。
如果我处理 Panel
的 MouseEnter
和 MouseLeave
事件,以及它的子项的 MouseEnter
和 MouseLeave
事件,事件按以下顺序引发:
Panel.MouseEnter
Panel.MouseLeave
Child1.MouseEnter
Child1.MouseLeave
Panel.MouseEnter
Panel.MouseLeave
但我需要以下顺序:
Panel.MouseEnter
Child1.MouseEnter
Child1.MouseLeave
Panel.MouseLeave
这可能吗?
最佳答案
如果您不介意创建用户控件(派生自面板或您希望的其他父容器),
覆盖你 parent 的 OnMouseLeave
方法,使其看起来像下面这样..
protected override void OnMouseLeave(EventArgs e)
{
if(this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
return;
else
{
base.OnMouseLeave(e);
}
}
然后,事件引发将按要求的顺序进行。
关于c# - 来自 Panel 及其子控件的 MouseEnter 和 MouseLeave 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2576021/