副屏关闭表单

Closing form in secondary screen

我有一个应用程序可以在主显示器和辅助显示器中打开两个表单。两个表单都有相同的代码,我对它们进行了设置,这样当我按下 Escape 键时,两个表单都应该关闭,但一个表单不会关闭。

代码:Form1

Public class Form1
Dim obj As New Form2
Dim obj2 As New Form3

Public Closecheck As Boolean = False

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
                Me.Hide()

                Obj2.Location = Screen.AllScreens(UBound(Screen.AllScreens)).Bounds.Location
                Obj.Show()
                Obj2.Show()
End Sub

Form2

Public Class Form2

Prive Sub Form2_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
            Form1.Closecheck = True
            Form3.Closeout3()
            Me.Close()
Form1.show
End if
    Public Sub Closeout2()
            If Form1.Closecheck = True Then
                MsgBox(Form1.Closecheck)
                Me.Close()
            End If
        End Sub

Form3

Public Class Form3

 Private Sub Form3_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Form1.Closecheck = True
            Form2.Closeout2()
            Me.Close()
Form1.show

End Sub
Public Sub Closeout3()
        If Form1.Closecheck = True Then
            MsgBox(Form1.Closecheck)
            Me.Close()
        End If

    End Sub

Form2 和 Form3 上的 MsgBox 只是让我知道他们正在寻找布尔值 Closecheck 但是当第二个 MsgBox 在其屏幕中打开时,整个屏幕是冻结,我无法关闭 MsgBox。很可能由于表格仍处于打开状态而到期。

这是一种使用 Form1 中声明的变量的方法。 Form2 和 Form3 简单地关闭自己,让 Form1 处理其余的事情。请注意 F2F3 是如何声明为 WithEvents.

表格 1:

Imports System.IO
Imports System.Resources

Public Class Form1

    Private WithEvents F2 As Form2
    Private WithEvents F3 As Form3

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If IsNothing(F2) Then
            F2 = New Form2
            F2.StartPosition = FormStartPosition.Manual
            F2.Location = Screen.AllScreens.First.Bounds.Location
        End If
        If IsNothing(F3) Then
            F3 = New Form3
            F3.StartPosition = FormStartPosition.Manual
            F3.Location = Screen.AllScreens.Last.Bounds.Location
        End If

        F2.Show()
        F3.Show()
        Me.Hide()
    End Sub

    Private Sub F2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles F2.FormClosed
        F2 = Nothing
        If Not IsNothing(F3) Then
            F3.Close()
        Else
            Me.Show()
        End If
    End Sub

    Private Sub F3_FormClosed(sender As Object, e As FormClosedEventArgs) Handles F3.FormClosed
        F3 = Nothing
        If Not IsNothing(F2) Then
            F2.Close()
        Else
            Me.Show()
        End If
    End Sub

End Class

表格 2:

Public Class Form2

    Private Sub Form2_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

End Class

表格 3:

Public Class Form3

    Private Sub Form2_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

End Class