单击当我点击动态用户控件控制事件不点火 [英] Click event is not firing when I click a control in dynamic usercontrol

查看:123
本文介绍了单击当我点击动态用户控件控制事件不点火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的用户控件不同的控件。动态负载用户控件在我的形式

I have different controls in my usercontrols. And load usercontrols dynamically in my form

UserControl2 usercontrol = new UserControl2();
usercontrol.Tag = i;
usercontrol.Click += usercontrol_Click;
flowLayoutPanel1.Controls.Add(usercontrol);

private void usercontrol_Click(object sender, EventArgs e)
{
   // handle event
}

当我点击用户控件控件的单击事件不点火。当我点击用户控件的空白区域仅火灾。

The click event is not firing when I click a control in usercontrol. It only fires when I click on empty area of usercontrol.

推荐答案

递归遍历所有的控件及导线上点击()事件的每个到相同的处理程序。从那里调用InvokeOnClick()。现在,点击任何东西会火的主要用户控件的Click()事件:

Recurse through all the controls and wire up the Click() event of each to the same handler. From there call InvokeOnClick(). Now clicking on anything will fire the Click() event of the main UserControl:

public partial class UserControl2 : UserControl
{

    public UserControl2()
    {
        InitializeComponent();
        WireAllControls(this);
    }

    private void WireAllControls(Control cont)
    {
        foreach (Control ctl in cont.Controls)
        {
            ctl.Click += ctl_Click;
            if (ctl.HasChildren)
            {
                WireAllControls(ctl);
            }
        }
    }

    private void ctl_Click(object sender, EventArgs e)
    {
        this.InvokeOnClick(this, EventArgs.Empty); 
    }

}

这篇关于单击当我点击动态用户控件控制事件不点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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