[VB.NET][2008] 2 forms show and close

41 Views Asked by At

I create 2 Forms and show them in different method, "Show" and "ShowDialog". I try to close the Form1 in the Form2 Load event, the DialogResult of the Form2 changed to "Cancel"? Is it a bug?

f1 is used for logo display and f2 is a login windows. f1 show and stay 3 seconds. f2 show and wait for user login.

Private WithEvents f1 As Form
    Private WithEvents f2 As Form
    Private WithEvents bg As System.ComponentModel.BackgroundWorker

    Public Sub Run()
        Try
            bg = New System.ComponentModel.BackgroundWorker
            f1 = New Form
            f1.Name = "Form1"
            f1.Show()
            Console.WriteLine("Show Form1 and stay 3 seconds")
            bg.RunWorkerAsync()

            f2 = New Form
            f2.Name = "Form2"
            Console.WriteLine("Show Form2 (ShowDialog)")
            Dim rlt = f2.ShowDialog()
            If rlt = DialogResult.Cancel Then
                Console.ReadKey()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub bg_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bg.DoWork
        Dim stw = Stopwatch.StartNew
        Do
            Application.DoEvents()
        Loop Until stw.ElapsedMilliseconds > 3000
    End Sub

    Private Sub bg_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bg.RunWorkerCompleted
        Console.WriteLine("Form2's DialogResult = {0}", f2.DialogResult)
        f1.Close()
        Console.WriteLine("After Form1 close, Form2's DialogResult changed to ({0}). WHY?", f2.DialogResult)
    End Sub

#VB.NET

0

There are 0 best solutions below