Leave/Enter events

  • Thread starter Thread starter Egil Winther
  • Start date Start date
E

Egil Winther

Hi All,

Is there a way to find out, on the leave-event, which control that is about
to receive focus?

Rgds
Egil Winther
ShipNet AS
 
You could use something like:

private Control ctlLeft = null;
private Control ctlEntered = null;
private void Control_Leave(object sender, System.EventArgs e) {
ctlLeft = (Control)sender;
}
private void Control_Enter(object sender, System.EventArgs e) {
ctlEntered = (Control)sender;
}
And then assign this method to all the enter and leave events on your form
(selecting them all and in the properties window selecting it from the
combobox)

This way you will always have a pointer to the recently left and recently
entered control.

You have to consider that the Leave event runs even before the other control
takes the focus so what you need to do you should do it in the Enter event
knowing which control you left. This way you can go back to that control and
do anything else you want with it.

Also remember you can add as many event handlers as you want to the Enter
event if you do it by code, so even if you assign this event handler to all
your control you can still assign another handler to do whatever you need to
do for each specific control. You just need to use the following line of
code to do the assignment:

this.textBox1.Leave += new System.EventHandler(this.Control_Leave);
this.textBox1.Enter += new System.EventHandler(this.Control_Enter);

Hope this works.
 
Back
Top