UserControl 的标签隐藏 MouseEnter 事件 [英] Label of UserControl is hiding MouseEnter event

查看:32
本文介绍了UserControl 的标签隐藏 MouseEnter 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法激活事件鼠标进入,因为标签在顶部.我尝试分配相同的事件,但是当我调用 MyUserControl myUserControl = (MyUserControl)sender;导致错误.这是我的代码:

I can't active the event mouse enter because label's on top. I try assign the same event, but when I call MyUserControl myUserControl = (MyUserControl)sender; results in error. Here's my code:

foreach (Control ctrl in MyUserControl.Controls)
{
     ctrl.MouseEnter += MyUserControl_MouseEnter;
}    

private void MyUserControl_MouseEnter(object sender, EventArgs e)
{
   MyUserControl myUC = (MyUserControl)sender;
   int test = myUC .Codigo;
}

事件 (Form_MouseEnter) 在表单中发生时起作用,但在组件中它返回类似System.InvalidCastException"的错误

the event (Form_MouseEnter) works when it occurs in the form, but in the components it returns an error like 'System.InvalidCastException'

public partial class MyUserControl : UserControl
{
    int g_intCodEquip;
    public int Codigo
    {
        set { g_intCodEquip = value; }
        get { return g_intCodEquip; }
    }
}

推荐答案

为了确保 MyUserControl 的任何和所有子控件都得到正确处理,我们可以迭代 MyUserControl 的控件树并订阅 MouseEnter 事件.

To ensure that any and all child controls of MyUserControl will be correctly handled, we can iterate the control tree of MyUserControl and subscribe to the MouseEnter event.

我们将所有这些事件路由到集中式 Any_MouseEnter 处理程序,该处理程序依次触发 Form1 订阅的新自定义事件:

We route all of these events to a centralized Any_MouseEnter handler which in turn fires a new custom event that the Form1 subscribes to:

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
        IterateControlTree();
    }

    void IterateControlTree(Control control = null)
    {
        if (control == null)
        {
            control = this;
        }
        control.MouseEnter += Any_MouseEnter;
        foreach (Control child in control.Controls)
        {
            IterateControlTree(child);
        }
    }

    private void Any_MouseEnter(object sender, EventArgs e)
    {
        // Before calling Invoke we need to make sure that
        // MyUserControlMouseEnter is not null as would be
        // the case if there are no subscribers to the event.
        // The '?' syntax performs this important check.
        MyUserControlMouseEnter?.Invoke(this, EventArgs.Empty);
    }

    // A custom event that this custom control can fire.
    public event EventHandler MyUserControlMouseEnter;

    public int Codigo
    {
        set
        {
            test = value;
        }
        get
        {
            return test;
        }
    }
    int test = 0;
}

注意:这是您之前的帖子 所以我复制了 'Codigo' 属性.

Note: This is a follow-up question to your previous post so I copied over the 'Codigo' property.

好的,现在在主 Form1 中,我们订阅由 MyUserControl 触发的新事件.现在发送者输入MyUserControl,转换成功,无论鼠标进入哪个控件,通知都有效.

OK, so now in the main Form1, we subscribe to the new event fired by MyUserControl. Now the sender is type MyUserControl, the cast succeeds, and the notification works no matter which control the mouse enters.

private void MyUserControl_MouseEnter(object sender, EventArgs e)
{
    MyUserControl myUserControl = (MyUserControl)sender;
    Debug.WriteLine(
        "MouseEnter Detected: " + myUserControl.Name + 
        " - Value of Codigo is: " + myUserControl.Codigo);
}

作为测试运行器,我们可以设置一个 4 x 3 的 MyUserControl 数组(工作示例 MyUserControl 包含一个标签和一个按钮).

As a test runner, we can set up a 4 x 3 array of MyUserControl (the working example MyUserControl contains both a Label and a Button).

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel() { ColumnCount = 4, RowCount = 4, Dock = DockStyle.Fill };
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        Controls.Add(tableLayoutPanel1);
        int row, column;
        for (int count = 0; count < 12; count++)
        {
            row = count / 4; column = count % 4;

            MyUserControl myUserControl = new MyUserControl();
            myUserControl.Name = "MyUserControl_" + count.ToString("D2"); // Name it! (Default is "") 

            // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv  
            // Subscribe to custom event fired by MyUserControl             
            myUserControl.MyUserControlMouseEnter += MyUserControl_MouseEnter;
            // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        

            myUserControl.Codigo = 1000 + count;                         // Example to set Codigo

            tableLayoutPanel1.Controls.Add(myUserControl, column, row);
        }
    }
}

行为遵循此10 秒 剪辑.

这篇关于UserControl 的标签隐藏 MouseEnter 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆