c# - 来自 Panel 及其子控件的 MouseEnter 和 MouseLeave 事件

标签 c# .net winforms mouseevent

我有一个包含子控件的 Panel

如果我处理 PanelMouseEnterMouseLeave 事件,以及它的子项的 MouseEnterMouseLeave 事件,事件按以下顺序引发:

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/

相关文章:

c# - 使用 SSH.NET 恢复网络连接后自动恢复 SFTP 下载

.net - 线程池最多使用 200 个线程

winforms - 格式化 WinForm 标签中的文本

c# - "Cannot access disposed object."异常问题解决

c# - 在 c# 面板上使用 DataReader 和 TextBox 控件的 Repeater 控件

c# - MEF 条件导入

c# - 使用 lambda 函数从旧列表创建新列表

c# - 为两个订单 ID 的排列创建唯一哈希码

c# - 如何将包含 AM/PM 的字符串转换为 DateTime?

c# - 如何在 Xamarin.iOS 中向后滑动?