简体   繁体   中英

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?

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.

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM