5 Answers
5

You can subscribe to the Control.Leave event which will be fired when the control loses focus. Originally, I thought using Control.LostFocus would be the most appropriate event to use but it is not available via the designer meaning you would need to manually subscribe to the event which is a bit ugly and unconventional in my opinion.

You sound like you're interested in Control.Validating. The advantage of using Control.Validating is that you can utilize the event handlers given argument; CancelEventArgs and set the Cancel property to true. What this will do is stop the control from losing focus and forcing the user to enter a valid value. I don't think this is appropriate for your application as you are not really validating anything but formatting the input.

Bare in mind that when the form closes, all controls sub-sequentially lose focus and the Control.Validating event is fired which could stop the Form closing until all fields pass their relative validation checks. If you find yourself needing to avoid this behavior a quick search will prevail.

I'd suggest using the Leave because I assume you aren't validating the value, but formatting it. Validating and Validated should contain code for validation and the aftermath of validation respectively, IMO.

As said by MSDN, When you change the focus by using the keyboard (TAB, SHIFT+TAB, 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.