用户控制单击 - Windows 窗体

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)