哪个 UserControl 正在调用 Event MouseEnter? [英] Which UserControl's calling Event MouseEnter?

查看:23
本文介绍了哪个 UserControl 正在调用 Event MouseEnter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl 列表,我想知道哪个 UserControl 正在调用事件 MouseEnter.我在 TableLayoutPanel 上添加了多个 UserControl.

I have a list of UserControl and I want to know which UserControl is calling event MouseEnter. I add multiple UserControls on TableLayoutPanel.

List<MyUserControl> form = new List<MyUserControl>();

for (int x = 0; x < dt.Rows.Count; x++)
{
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 200));
    if (x == 0)
       tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
    form.Add(new MyUserControl());
}

for (int x = 0; x < form.Count; x++)
{    
    form[x].MouseEnter += new EventHandler(Form_MouseEnter);
    tableLayoutPanel1.Controls.Add(form[x], x, 0);
}

如何找出哪个 UserControl 激活了该事件?

How do I find out which UserControl activated the event?

推荐答案

最大的不同是如果你给你的新 MyUserControl 一个 Name,因为默认 Name 是一个空字符串.您可以尝试将代码更改为此,看看是否有帮助吗?

The thing that should make the biggest difference is if you give a Name to your new MyUserControl because the default Name is an empty string. Could you try changing your code to this and see if it helps?

List<MyUserControl> form = new List<MyUserControl>();

for (int x = 0; x < 5; x++)
{
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 200));
    if (x == 0)
        tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));

    // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    // Here are the changes
    MyUserControl myUserControl = new MyUserControl();
    myUserControl.Name = "MyUserControl_" + x.ToString("D2"); // Name it! (Default is "")                
    myUserControl.MouseEnter += MyUserControl_MouseEnter;     // Hook the MouseEnter here
    myUserControl.Codigo = 1000 + x;                          // Example to set Codigo     
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    form.Add(myUserControl); // NOTE! This is changed from 'new MyUserControl()'.
}

for (int x = 0; x < form.Count; x++)
{
    tableLayoutPanel1.Controls.Add(form[x], x, 0);
}

现在处理程序看起来像这样:

Now the handler looks like this:

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

...哪里(根据您对 Codigo 的评论)...

... where (based on your comment about Codigo) ...

class MyUserControl : UserControl
{
    public int Codigo 
    { 
        set 
        { 
            test = value; 
        } 
        get 
        { 
            return test; 
        } 
    }
    int test = 0;
    // Of course there is more implementation of MyUserControl that follows...
}

我真的希望这能帮助您解决您遇到的问题.

I really hope this helps you fix the problem you're having.

这篇关于哪个 UserControl 正在调用 Event MouseEnter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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