User Control Click - Windows Forms

前端 未结 2 1501
不思量自难忘°
不思量自难忘° 2020-12-19 04:03

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 w

相关标签:
2条回答
  • 2020-12-19 04:39

    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;
    }
    

    After this piece of code workd, MyUserControl_Click will fire when any control on the user control is clicked.

    0 讨论(0)
  • 2020-12-19 04:47
        foreach (Control c in this.Controls)
        {
            c.Click += new EventHandler(SameAsForm_Click);
        }
    

    Keep in mind that this won't add labels' clickevents in groupboxes, panels etc to the "SameAsForm_Click"-EventHandler.

    0 讨论(0)
提交回复
热议问题