里面控制文本时,点击用户控制click事件不工作? [英] User control click event not working when clicking on text inside control?

查看:118
本文介绍了里面控制文本时,点击用户控制click事件不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了里面的标签叫做GameButton用户控件。当我在用户控件添加到我的形式,和一个点击事件添加到它,它当你点击自定义按钮的背景下引发的,而不是在标签的文本? ?我将如何解决这个问题,不添加一堆click事件的用户控件代码中

I have a user control called GameButton that has a label inside it. When I add the user control to my form, and add a click event to it, its triggered when you click on the background of the custom button, but not the text in the label? How would I fix this without adding a bunch of click events inside the user controls code?

编辑:UI框架:的WinForms

edit: UI framework: winforms

推荐答案

如果我理解您正确,你的用户控件GameButton单击时,而不是当点击该标签会触发事件 - 而您想要。这是因为在标签(控制)是在背景之上。因此,你需要注册与click事件的标签为好。这可以手动在设计或编程来完成页面上每个控件。

If I am understanding you properly, your GameButton usercontrol will fire the event when clicked on, but not when the label is clicked on -- and you want both. This is because the label (a control) is on top of the background. Therefore, you need to register your label with the click event as well. This can be done manually in the designer or programmatically for each control on the page.

如果你想要做在该用户的每个控件,将其放入该用户控件的onload事件,你可以使用相同的单击事件对于每个控件:

If you want to do EVERY control in the UserControl, put this into the UserControl's OnLoad event and you can use the same click event for every control:

foreach(Control c in this.Controls){
    c.Click += new EventHandler(yourEvent_handler_click);
}

public void yourEvent_handler_click (object sender, EventArgs e){
    //whatever you want your event handler to do
}

编辑:最好的办法是建立在用户控件的Click事件处理程序属性。这样一来,每次添加时间/删除一个click事件用户控件,它添加/删除它自动在用户控件中的所有控件。

The best way is to create the click event handler property in the user control. This way, every time you add/remove a click event to your user control, it adds/removes it to all the controls within the user control automatically.

public new event EventHandler Click {
        add {
            base.Click += value;
            foreach (Control control in Controls) {
                control.Click += value;
            }
        }
        remove {
            base.Click -= value;
            foreach (Control control in Controls) {
                control.Click -= value;
            }
        }
    }

这是根据另一个< A HREF =htt​​p://stackoverflow.com/questions/5820490/click-event-for-net-windows-forms-user-control>帖子:

希望这有助于!

这篇关于里面控制文本时,点击用户控制click事件不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆