I have a user control consisting of many children controls, and I need to handle the user control's double-click event. So I add the event handler but it's never triggered.
The quickest solution I am finding is to iterate through the children controls and subscribe to their double-click events. Is this really the right way to do it? Are there better ways? Thank you.
Best Answer
The following will work only for the first level children control which are being added to the user control. It will not work for the controls which are added subsequently to these children. For example, it will work for a double-click on a first level
Panel
child control, but not on aLabel
which is added to the panel.Test.cs
Form1.cs
To address the other case, when you have controls contained inside controls, you can use the following code. Be sure to add the user control to it's parent control before adding any other child controls to it. Once you do that, follow the same principle as you go down the control hierarchy.
To put it simpler, before adding a control anywhere in the control hierarchy, make sure that it's parent is already added to a control collection.
Test.cs
Form1.cs