User Control Click - Windows Forms(用户控制单击 - Windows 窗体)
问题描述
我的 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 窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用户控制单击 - Windows 窗体


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04