Help and Support
 

powered byLive Search

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
Last Review:December 3, 2007
Revision:1.2
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.

Back to the top

CAUSE

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

Back to the top

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.

Back to the top

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

Back to the top

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.

Back to the top


APPLIES TO
Microsoft .NET Framework 2.0
Microsoft .NET Framework 1.1

Back to the top

Keywords: 
kbtshoot kbprb kbdev kbcode KB905719

Back to the top

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by E-mail, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.