Search code examples
vb.netwinformsmultiple-monitors

Closing form in secondary screen


I have a application that opens two forms in the primary and secondary monitors. Both Forms have the same code and I have them setup so that when I hit the Escape key both forms should close, but one forms does not close.

Code: 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

The MsgBox on Form2 and Form3 are just there to let me know that they are finding the Booloean Closecheck But when the second MsgBox opens in its screen the entire screen is frozen and I can not close the MsgBox. Due most likely by the fact the Form is still open.


Solution

  • Here's one way to do it using the variables declared in Form1. Form2 and Form3 simply close themselves and let Form1 take care of the rest. Note how F2 and F3 have been declared as 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
    

    Form2:

    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