Search code examples
vb.netwinformsfocusmdimdichild

GotFocus() and MDI Children


I have an MDI form with 3 child windows.

  • The MDI starts empty.

  • Each child can be opened once from the menu.

When I open multiple windows, and close the top one should get focus, but the new active child-form's GotFocus() event is not firing.

Private Sub frmMain_gotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
...
End Sub

Solution

  • Unlucky guess. Either the Enter or the Activated event will solve your problem.

    In Windows Forms programming, you'd typically want to avoid the GotFocus and LostFocus events. They are often hidden in the designer, but not consistently. Respectively, the Enter and Leave events are their replacements, they are generated from the logical state of the UI instead of the raw Windows messages. Makes a difference when using Validating and MDI.

    Activated is the "natural" one since the actual focus moves to a child control of that form.