Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the LostFocus event get called at different times?

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?

like image 402
Kyle Baran Avatar asked Apr 15 '14 20:04

Kyle Baran


2 Answers

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.

like image 76
Paul Sweatte Avatar answered Sep 20 '22 16:09

Paul Sweatte


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.

like image 23
Ben H Avatar answered Sep 23 '22 16:09

Ben H