Yaroslav
Yaroslav

Reputation: 9

Click Windows Form

Hello everyone There is a form that is inherited from other forms I signed this form for the Click event, but it doesn't work for child controls, only for the form itself. In my opinion, it is necessary to register some property. Tell me, please

Upvotes: 0

Views: 88

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

You need to subscribe to control events separately, you can do that recursively for every child control using the following code sample:

void SubscribeToControlsEventsRecursive(ControlCollection collection)
 { 
    foreach (Control c in collection)  
     {  
       c.MouseClick += (sender, e) => {/* handle the click here  */});  
       SubscribeToControlsEventsRecursive(c.Controls);
     }
 }

Upvotes: 0

M. Elghamry
M. Elghamry

Reputation: 322

In Windows Forms, contained controls don't inherit the registered events of container control, you'll need to register the event for the controls you want.

You can loop in all the controls to register your intended event:

foreach (Control control in this.Controls)
{
    control.Click += myForm_Click;
}

Upvotes: 1

Related Questions