且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单击当我点击动态用户控件控制事件不点火

更新时间:2023-12-06 11:53:52

递归遍历所有的控件及导线上点击()事件的每个到相同的处理程序。从那里调用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); 
    }

}