Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 905719 - Last Review: December 3, 2007 - Revision: 1.2
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
On This Page
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, "ok")
- 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("ok")
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 "Applies to" section.
MORE INFORMATION
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.
- Paste the following code sample in the Form1 form.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
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 = "Button1"
Me.Button1.Size = New System.Drawing.Size(128, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Show ChildForm"
'
'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 = "Form1"
Me.Text = "ParentForm"
Me.ResumeLayout(False)
End Sub
#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
- Add a form that is named Form2 to the project.
- Paste the following code sample in the Form2 form.
Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
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 = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Show MsgBox"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.ShowInTaskbar = False
Me.Text = "ChildForm"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("ok")
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
- Press F5 to run the project.
- Click Show ChildForm, click Show MsgBox, and then click OK.
- Repeat step 7.
APPLIES TO
- Microsoft .NET Framework 2.0
- Microsoft .NET Framework 1.1
| kbtshoot kbprb kbdev kbcode KB905719 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please