6

Which event should I use for this?

3 Answers 3

17

Control.Leave or Control.LostFocus:

When you change the focus by using the keyboard (, +, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

  1. Enter
  2. GotFocus
  3. Leave
  4. Validating
  5. Validated
  6. LostFocus

When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

  1. Enter
  2. GotFocus
  3. LostFocus
  4. Leave
  5. Validating
  6. Validated

If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

2
  • This is strange, when I lose the focus with mouse, I don't get Validated event to fire for some reason.
    – Joan Venge
    Nov 23, 2009 at 22:20
  • Some controls may not fire it; also CausesValidation may be set to false. And Validated is not strictly a focus event, as in, you shouldn't use it as a "focus lost" event. It's for validation purposes.
    – Joey
    Nov 23, 2009 at 23:10
1

Control.LostFocus.

3
  • 2
    It's not listed under TextBox property editor. Do you know why?
    – Joan Venge
    Nov 23, 2009 at 22:07
  • 2
    Just not shown in the property editor. You can add it from code behind. If Control.Leave is there as Johannes suggested you could use that as well.
    – Wil P
    Nov 23, 2009 at 22:12
  • 5
    It is not shown in the properties window because the event is marked with [BrowsableAttribute(false)] msdn.microsoft.com/en-us/library/… Nov 23, 2009 at 22:14
0

'Control.LostFocus'

1
  • 2
    It's not listed under TextBox property editor. Do you know why?
    – Joan Venge
    Nov 23, 2009 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.