BUG: Focus Does Not Return to an MDI Parent Form

Last reviewed: February 10, 1998
Article ID: Q180747
The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0

SYMPTOMS

An MDI child form opens a second modal form and you then switch to another Windows program. When you switch back to the second modal form and close the modal form, the focus goes to the other Windows program instead of the MDI child form.

RESOLUTION

There are two possible workarounds for this problem:

  • Set the focus to the MDI child form after showing the non-MDI child form with the SetFocus method.

    -or-

  • Use the SetForegroundWindow API in the unload event of the modal form. The MORE INFORMATION section below shows you how to create a project that demonstrates this problem. If you want this sample project to include the workaround, add the following code to Form2:

          Option Explicit
          Private Declare Function SetForegroundWindow Lib "user32" _
    
            (ByVal hwnd As Long) As Long
    
          Private Sub Form_Unload(Cancel As Integer)
             SetForegroundWindow MDIForm1.hwnd
          End Sub
    
    

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default. Set the MDIChild property of Form1 to True.

  2. Add an MDI form to the project by completing the following steps:

    a. From the Project menu, click Add MDI form to display the Add MDI Form

          dialog box.
    

    b. From the New tab, check that MDI form is highlighted.

    c. Click Open to close the Add MDI Form dialog box.

  3. Add a second form to the project.

  4. Add a CommandButton to Form1.

  5. Copy the following code to the Code window of Form1:

          Option Explicit
    

          Private Sub Command1_Click()
    
             Form2.Show vbModal
          End Sub
    
    

  6. Start another Windows program, such as Windows Explorer.

  7. Switch back to Visual Basic. On the Run menu, click Start or press the F5 key to start the program. Click the Command Button to display the Second form. Switch to the other Windows program using the ALT+TAB KEYS. Switch back to the modal from with the ALT+TAB keys. Close the modal form. Note that the focus goes to the Windows program rather than the MDI Parent form.
Keywords          : vb5all VBKBObj VBKBVB
Version           : WINDOWS:5.0
Platform          : WINDOWS
Issue type        : kbbug
Solution Type     : kbpending


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 10, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.