10

In reference to this MSDN page (or any related page on the matter), it states that:

When you change the focus by using the keyboard, focus events occur in the following order:

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

However, when you use the mouse to raise events, the order changes!

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

Wouldn't this make the chain of events completely different? My interpretation here is that the keyboard chain ensures everything is in working order, then raises the LostFocus event. Yet, the mouse events seem to raise it before validating for some reason. Why is that?

Kyle Baran
  • 1,793
  • 2
  • 15
  • 30
  • Is that Caution! in the MSDN article you linked worded strong enough? Never use LostFocus, only Leave. Now it doesn't matter anymore. – Hans Passant Apr 15 '14 at 20:14
  • I'm not setting the focus, though. I'm just curious as to why the order changes based on the input method. – Kyle Baran Apr 15 '14 at 20:17
  • 4
    The keyboard navigation must be in this order in order to apply the validations. Those are intended to react to them in order to validate any inputted strings. Meaning, If You tab through Your "text-boxes" You will receive other order of events, then navigating via mouse, what enables YOu to react DIRECTLY after issueing some input. Did that help You ? – icbytes May 12 '14 at 15:58
  • 1
    Yes, it did. I hadn't realized at the time of writing this question that keyboard/mouse validation of events could be (should be?) very different. – Kyle Baran May 13 '14 at 02:31

2 Answers2

1

As noted above:

In the MSDN article you linked worded strong enough? Never use LostFocus, only Leave.

The keyboard navigation must be in this order in order to apply the validations. Those are intended to react to them in order to validate any input strings.

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
1

The best example I can think of is the e.Cancel aspect in validation. Using the keyboard for navigation is usually a control to control type of navigation (including child and parent controls). Using the mouse for form navigation does not always result in a control being selected. For example closing a form or simply click outside of the control (i.e. re-positioning the form). It is not always desirable to have the validation occur when a mouse click occurs outside a control. Hope that helps.

Ben H
  • 19
  • 2