Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单击事件时获取UserControl_C#_Vb.net_Visual Studio - Fatal编程技术网

C# 单击事件时获取UserControl

C# 单击事件时获取UserControl,c#,vb.net,visual-studio,C#,Vb.net,Visual Studio,我创建了一个自定义控件,其中包含面板、标签和图片框。我试图在调用其单击事件时获取用户控件name属性 在我的用户控件中,我有以下代码来冒泡单击事件,以便任何子控件都可以触发单击事件 private void reclusiveControlLoop(ControlCollection Controls, EventHandler EventValue, Boolean Mode) { foreach (Control control in Controls)

我创建了一个自定义控件,其中包含
面板
标签
图片框
。我试图在调用其单击事件时获取
用户控件
name属性

在我的用户控件中,我有以下代码来冒泡
单击
事件,以便任何子控件都可以触发
单击
事件

    private void reclusiveControlLoop(ControlCollection Controls, EventHandler EventValue, Boolean Mode)
    {
        foreach (Control control in Controls)
        {
            if (control.Controls.Count > 0)
            {
                reclusiveControlLoop(control.Controls, EventValue, Mode);
            }

            if (Mode)
            {
                control.Click += EventValue;
            }
            else
            {
                control.Click -= EventValue;
            }
        }
    }

    public new event EventHandler Click
    {
        add
        {
            base.Click += value;
            reclusiveControlLoop(pnlContent.Controls, value, true);
        }
        remove
        {
            base.Click -= value;
            reclusiveControlLoop(pnlContent.Controls, value, false);
        }
    }
在具有用户控件的窗体上,我添加了以下代码

Private Sub ListLinkLaunch_Click(sender As Object, e As EventArgs) Handles llCommissions.Click
        Try
            Dim llItem As Control = CType(sender, Control)
            Dim sName As String = llItem.Name.ToString()

            MessageBox.Show(sName)

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

问题是它没有返回我添加的控件的名称,即
llCommissions
,而是返回用户控件标签的名称
lblListLinkTitle

获取控件名称的原因(例如,
lblListLinkTitle
)与将
单击事件分配给每个控件的方式有关。在代码运行时,每个控件都会将
Click
事件一直提升到包含表单

要获得所需的行为,每个控件都应该将其单击事件提升到自定义控件,然后从该控件向包含窗体提升一个新事件

要使其按您的需要工作,请进行以下更改:

调用InitializeComponent后,UserControl.cs-构造函数内部的

pnlContent.Click += AllControls_Click;
reclusiveControlLoop(pnlContent.Controls, true);
并且,这是您的代码的其余部分,经过修改,工作方式略有不同

private void AllControls_Click(object sender, EventArgs e)
{
    base.OnClick(e);
}

private void reclusiveControlLoop(ControlCollection Controls, Boolean Mode)
{
    foreach (Control control in Controls)
    {
        if(control.Controls.Count > 0)
        {
            reclusiveControlLoop(control.Controls, Mode);
        }

        if(Mode)
        {
            control.Click += AllControls_Click;
        }
        else
        {
            control.Click -= AllControls_Click;
        }
    }
}
使用此方法,您不再需要定义新的Click EventHandler(
public new EventHandler Click

这在实践中意味着,在表单上,您永远不会知道单击了哪个控件(
Label
PictureBox
),应该是这样的,这些详细信息不应该传递给根表单