关闭二级屏幕中的表单

时间:2015-07-27 15:33:28

标签: vb.net winforms multiple-monitors

我有一个应用程序在主要和辅助监视器中打开两个表单。两个表单都有相同的代码,我设置它们,这样当我按下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

窗体2

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就是让我知道他们正在找到Booloean Closecheck但是当第二个MsgBox在其屏幕上打开时,整个屏幕都被冻结了我无法关闭MsgBox。最有可能的是表格仍然是公开的。

1 个答案:

答案 0 :(得分:0)

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

Form1中:

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

Form3:

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