close form

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I've made an app with vb 2010, which holds a mainform. On it is a menutoolstrip with button, when I click on that button a second form is opened on a second monitor and the button is being checked (different color).

When I click the button again, it is unchecked, but the form on the second monitor is still open, while it should be closed.

Here is the code I use:
VB.NET:
Private Sub ButtonItem16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem16.Click
        If ButtonItem16.Checked = True Then
            ButtonItem16.Checked = False
        Else
            ButtonItem16.Checked = True
        End If
    End Sub

    Private Sub ButtonItem16_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem16.CheckedChanged
        Dim screen As Screen
        Dim scfrm As New Form
        Dim lab As New Label
        If screen.AllScreens.Length > 1 Then
            If ButtonItem16.Checked = True Then
                scfrm.Controls.Add(lab)
                lab.Dock = DockStyle.Fill
                'Show the form on second screen
                screen = screen.AllScreens(1)
                scfrm.StartPosition = FormStartPosition.Manual
                scfrm.Location = screen.Bounds.Location + New Point(100, 100)
                scfrm.Show()
                scfrm.WindowState = FormWindowState.Maximized
                scfrm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
                scfrm.BackColor = Color.Black
            Else
                scfrm.Close()
            End If
        Else
            MsgBox("No extended screen found, dual video needs an extended screen.")
            ButtonItem16.Checked = False
        End If
    End Sub

Hope someone can help me with this?

Thanks
 
If you expect the existing form to be closed then you have to close the existing form. You're not though. You're creating a new form of the same type and then closing the new one. The existing one is unaffected. When you create a new form the first time to open it, you need to assign it to a variable so that you can access it later on.

Also, what happens if the user closes the form first? Your tool bar button will still be checked as though it was open. You should also handle the FormClosed event of the form and uncheck the button.
 
Back
Top