Microsoft KB Archive/905719

= The wrong application may come to the foreground when you close a modal dialog box in a Windows Forms-based application on the .NET Framework 2.0 or on the .NET Framework 1.1 =

Article ID: 905719

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft .NET Framework 2.0
 * Microsoft .NET Framework 1.1

-





SYMPTOMS
Consider the following scenario. In a Microsoft Windows Forms-based application, a top-level form owns a pop-up window, and the pop-up window raises a modal dialog box. In this scenario, the wrong application may come to the foreground when you close the modal dialog box. The top-level form that owns the pop-up window may not come to the foreground as expected.

This problem occurs when the pop-up window closes itself explicitly or when the pop-up window creates a modal dialog box in the pop-up form's Closing event. This problem occurs on a computer that has the Microsoft .NET Framework 2.0 or the Microsoft .NET Framework 1.1 installed.



CAUSE
This problem may occur if the pop-up window closes immediately after the user closes the modal dialog box.



WORKAROUND
To work around this problem, use one of the following methods:   Set the top-level form as the owner of the modal dialog box. To do this, you can use code that is similar to the following. MessageBox.Show(Me.Owner, &quot;ok&quot;)   Set the Owner property of the pop-up window to Nothing before you display the modal dialog box. To do this, you can use code that is similar to the following. Private Sub Button1_Click(...) Dim f As Form f = Me.Owner Me.Owner = Nothing MessageBox.Show(&quot;ok&quot;) Me.Owner = f       Me.Close End Sub Note When you set the Owner property of the pop-up window to Nothing, the focus is correctly reset to the active window or to the pop-up window. 



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to reproduce the problem
 On a computer that has the .NET Framework 2.0 or the .NET Framework 1.1 installed, start two or more applications. Use Microsoft Visual Basic .NET to create a Windows Forms-based application.</li>  Paste the following code sample in the Form1 form. Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

Public Sub New MyBase.New

'This call is required by the Windows Form Designer. InitializeComponent

'Add any initialization after the InitializeComponent call

End Sub

'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub

'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout '       'Button1 '       Me.Button1.Location = New System.Drawing.Point(80, 88) Me.Button1.Name = &quot;Button1&quot; Me.Button1.Size = New System.Drawing.Size(128, 23) Me.Button1.TabIndex = 0 Me.Button1.Text = &quot;Show ChildForm&quot; '       'Form1 '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.Button1) Me.IsMdiContainer = True Me.Name = &quot;Form1&quot; Me.Text = &quot;ParentForm&quot; Me.ResumeLayout(False)

End Sub


 * 1) End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim f As New Form2 f.Owner = Me       f.Show End Sub

End Class </li> Add a form that is named Form2 to the project.</li>  Paste the following code sample in the Form2 form. Public Class Form2 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

Public Sub New MyBase.New

'This call is required by the Windows Form Designer. InitializeComponent

'Add any initialization after the InitializeComponent call

End Sub

'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub

'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout '       'Button1 '       Me.Button1.Location = New System.Drawing.Point(96, 104) Me.Button1.Name = &quot;Button1&quot; Me.Button1.Size = New System.Drawing.Size(96, 23) Me.Button1.TabIndex = 0 Me.Button1.Text = &quot;Show MsgBox&quot; '       'Form2 '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.Button1) Me.Name = &quot;Form2&quot; Me.ShowInTaskbar = False Me.Text = &quot;ChildForm&quot; Me.ResumeLayout(False)

End Sub


 * 1) End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show(&quot;ok&quot;) Me.Close End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub End Class </li> Press F5 to run the project.</li> Click Show ChildForm, click Show MsgBox, and then click OK.</li> Repeat step 7.</li></ol>

Additional query words: loses Activation

Keywords: kbtshoot kbprb kbdev kbcode KB905719

-

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

© Microsoft Corporation. All rights reserved.