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 窗体
基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01