Event listeners?

  • Thread starter Thread starter Deena
  • Start date Start date
D

Deena

Hi

How do I create event listeners? I would like to listen for the "GotFocus"
event for each control on a form.

Deena
 
Hi Deena,
How do I create event listeners? I would like to listen for the "GotFocus"
event for each control on a form.

Select all controls on a form, and double click on "Enter" event in
"event properties" or type e.g. "controlItem_Enter".
There is "GotFocus" event... but it is hidden :(

// e.g. handler of "Enter"
private void controlItem_Enter(object sender, EventArgs e) {
if( sender==textBox1 ) {
// textBox1 was selected
}
...
else if( sender==textBox10 ) {
// textBox10 was selected
}
}

Regards

Marcin
 
check out the delegate keyword on msdn

you should create event delegates and attach to the handlers

e.g.:

public Constructor(){
foreach (Control control in this.Controls){
control.GotFocus += new EventHandler(GotFocus_EventHandler);
}
}

private void GotFocus_EventHandler(object sender, EventArgs e){
// EVENT HANDLER IMPLEMENTATION GOES HERE ...
}


Murat
 
Deena,
Just remember that controls can be nested inside of controls (option buttons
in a group box), so you many need to make the for each control in controls a
recursive function.

Hope this helps
Jay
 
Back
Top