determine which control will get the focus

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When handling the Lost_Focus event, is there a way to determine which control
will get the focus before leaving the Lost Focus handler ? and then prevent
the current control to not loose the focus ?
 
Michael said:
When handling the Lost_Focus event, is there a way to determine which control
will get the focus before leaving the Lost Focus handler ? and then prevent
the current control to not loose the focus ?

You may want to instead subscribe to the control's Leave event. According
to MSDN: "CAUTION Do not attempt to set focus from within the LostFocus event
handler. Doing so can cause your application or the operating system to stop
responding."

To ensure your control doesn't lose focus, place the following line in the
Leave event handler:

this.myControl.Focus();

You can detect which control in your form gains focus by checking the form's
ActiveControl property. Note that if the user switches focus to another
form, the control's Leave event will not be raised. In that case, you should
probably handle the form's Deactivate event.
 
Back
Top