For every event I have to write an event handler:
this.MouseMove += new MouseEventHandler(Home_MouseMove);
this.KeyPress += new KeyPressEventHandler(Home_KeyPress);
What if I one to capture every button handler, then I would have to write a event handler for each buttons.
Any good method?
You can attach an event for every control of type
Buttonon theForm_Loadevent. Even better, in the constructor itself after the call toInitializeComponent.Why ‘after’? Because if you do before, there will be no control on the form to attach to.
Note that if you have a
Panelcontrol with Buttons in it, this will not attach the event to it, as that button is not part of the Form (“this”) collection of.Controls; but ratherthis.Panel1.Controls. See?If this is the case for you, you can use recursion to iterate though each collection.