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 (905719)



The information in this article 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, "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

  1. On a computer that has the .NET Framework 2.0 or the .NET Framework 1.1 installed, start two or more applications.
  2. Use Microsoft Visual Basic .NET to create a Windows Forms-based application.
  3. 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
    
  4. Add a form that is named Form2 to the project.
  5. 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
    
    
  6. Press F5 to run the project.
  7. Click Show ChildForm, click Show MsgBox, and then click OK.
  8. Repeat step 7.

Modification Type:MajorLast Reviewed:11/3/2005
Keywords:kbDev kbtshoot kbcode kbprb KB905719 kbAudDeveloper