简体   繁体   English

用户控制单击 - Windows窗体

[英]User Control Click - Windows Forms

I have a custom user control on my windows forms. 我的Windows窗体上有自定义用户控件。 This control has a few labels on it. 此控件上有几个标签。

I will be dynamically displaying an array of these controls on my form which will contain different bits of data. 我将在我的表单上动态显示这些控件的数组,其中包含不同的数据位。

What I am trying to do is know which user control was selected when I click on it. 我想要做的是知道当我点击它时选择了哪个用户控件。

This works when I click on an empty space on the user control, however, if I click on any label on the user control it will not recognize the user control click. 当我单击用户控件上的空白区域时,这会起作用,但是,如果单击用户控件上的任何标签,它将无法识别用户控件单击。

Any thoughts on how I can do a full user control click, even if a label on the control is being clicked? 有关如何进行完全用户控制点击的任何想法,即使点击控件上的标签?

If this question is not clear, or you need more info, please leave a comment. 如果这个问题不明确,或者您需要更多信息,请发表评论。

I am doing this in c#. 我在c#中这样做。

Thanks! 谢谢!

User control's click event won't fire when another control is clicked on the user control. 在用户控件上单击另一个控件时,不会触发用户控件的单击事件。 You need to manually bind each element's click event. 您需要手动绑定每个元素的click事件。 You can do this with a simple loop on the user control's codebehind: 您可以通过用户控件的代码隐藏上的简单循环来执行此操作:

foreach (Control control in Controls)
{
    // I am assuming MyUserControl_Click handles the click event of the user control.
    control.Click += MyUserControl_Click;
}

After this piece of code workd, MyUserControl_Click will fire when any control on the user control is clicked. 完成这段代码后,当单击对用户控件的任何控件时,将触发MyUserControl_Click。

    foreach (Control c in this.Controls)
    {
        c.Click += new EventHandler(SameAsForm_Click);
    }

Keep in mind that this won't add labels' clickevents in groupboxes, panels etc to the "SameAsForm_Click"-EventHandler. 请记住,这不会将组框,面板等中的标签clickevent添加到“SameAsForm_Click”-EventHandler中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM