Kurz gesagt: In VB.NET 2003 solltest du für Windows-Forms fast immer Enter/Leave verwenden, nicht GotFocus/LostFocus. GotFocus und LostFocus sind niedrigere, technischere Fokus-Events; Enter und...
VB.NET 2003: GotFocus/LostFocus oder Enter/Leave?
Antwort vom**Use `Enter`/`Leave` for normal WinForms code; `GotFocus`/`LostFocus` are lower-level Windows message events and are usually the wrong choice.** Microsoft explicitly recommends `Enter`/`Leave` for controls, while `GotFocus`/`LostFocus` are mainly for custom-control or UI-cue scenarios. ([learn.microsoft.com](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.lostfocus?view=windowsdesktop-9.0)) ## The practical difference `GotFocus` and `LostFocus` are tied closely to the underlying Windows focus messages (`WM_SETFOCUS` / `WM_KILLFOCUS`). That makes them more “raw”. `Enter` and `Leave` are the higher-level WinForms events you normally want in application code. ([learn.microsoft.com](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.lostfocus?view=windowsdesktop-9.0)) The important consequence: `Enter`/`Leave` behave more consistently in container hierarchies. They are hierarchical, so focus changes can cascade through parent controls like `GroupBox` or other containers. `GotFocus`/`LostFocus` do not give you that same higher-level behavior. ([learn.microsoft.com](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.enter?view=windowsdesktop-10.0)) ## Event order matters When focus changes by keyboard navigation like Tab, the order is: - `Enter` - `GotFocus` - `Leave` - `Validating` - `Validated` - `LostFocus` When focus changes by mouse or by calling `Focus()`, the order is: - `Enter` - `GotFocus` - `LostFocus` - `Leave` - `Validating` - `Validated` So `Leave` vs. `LostFocus` is not just naming — they can fire in different order depending on how focus changed. That is exactly why validation or UI logic often behaves strangely if you hook the low-level events. ([learn.microsoft.com](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.lostfocus?view=windowsdesktop-9.0)) ## What to use in VB.NET 2003 For a normal textbox highlight, enabling/disabling buttons, or starting validation-related UI behavior, use `Enter` and `Leave`. That is the safe default. Use `GotFocus` and `LostFocus` only if you specifically need low-level focus-message behavior. In ordinary form code, choosing them usually creates edge cases rather than solving them. ([learn.microsoft.com](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.lostfocus?view=windowsdesktop-9.0)) A useful rule of thumb: - **UI behavior:** `Enter` / `Leave` - **Custom control internals:** `GotFocus` / `LostFocus` One more important point: do **not** try to set focus again from inside any of these handlers, because Microsoft warns that this can cause the app to hang. ([learn.microsoft.com](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.lostfocus?view=windowsdesktop-9.0))