Microsoft KB Archive/180747

= FIX: Focus Does Not Return to an MDI Parent Form =

Article ID: 180747

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition

-



This article was previously published under Q180747



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. This bug has been fixed in Visual Basic 6.0.



Steps to Reproduce Behavior
 Start a new Standard EXE project in Visual Basic. Form1 is created by default. Set the MDIChild property of Form1 to True. Add an MDI form to the project by completing the following steps:

 From the Project menu, click Add MDI form to display the Add MDI Form dialog box.</li> From the New tab, check that MDI form is highlighted.</li> Click Open to close the Add MDI Form dialog box.</li></ol> </li> Add a second form to the project.</li> Add a CommandButton to Form1.</li>  Copy the following code to the Code window of Form1: Option Explicit Private Sub Command1_Click Form2.Show vbModal End Sub

</li> Start another Windows program, such as Windows Explorer.</li> 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.</li></ol>

Additional query words: kbVBp500bug kbVBp600fix kbVBp kbdsd kbDSupport kbNoKeyWord

Keywords: kbbug kbfix KB180747

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.