用户控制点击 - Windows窗体 [英] User Control Click - Windows Forms
问题描述
我在我的Windows表单的自定义用户控件。这种控制上有几个标签。
I have a custom user control on my windows forms. 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.
我在C#这样做。
谢谢!
推荐答案
用户控件的Click事件被点击上的另一个控件时,将不会触发用户控件。你需要给每个元素的click事件手动绑定。您可以在用户控件的代码隐藏一个简单的循环做到这一点:
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. 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;
}
这一段代码workd后,MyUserControl_Click将触发对用户在任何控制控件被点击。
After this piece of code workd, MyUserControl_Click will fire when any control on the user control is clicked.
这篇关于用户控制点击 - Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!