问题描述
我的 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# 中这样做.
I am doing this in c#.
谢谢!
推荐答案
当在用户控件上单击另一个控件时,不会触发用户控件的单击事件.您需要手动绑定每个元素的点击事件.您可以在用户控件的代码隐藏上通过一个简单的循环来做到这一点:
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;
}
这段代码运行后,MyUserControl_Click 将在用户控件上的任何控件被单击时触发.
After this piece of code workd, MyUserControl_Click will fire when any control on the user control is clicked.
这篇关于用户控制单击 - Windows 窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!