How do I close a form when a user clicks outside the form's window?

前端 未结 8 2073
孤独总比滥情好
孤独总比滥情好 2021-01-12 15:51

I want to close a System.Windows.Forms.Form if the user clicks anywhere outside it. I\'ve tried using IMessageFilter, but even then none of the messages are passed to PreFi

8条回答
  •  無奈伤痛
    2021-01-12 16:36

    SIMPLY WAY : on Form1 use this code to call form2:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles     Button1.Click
        Form2.Owner = Me
        Form2.Show()
    End Sub
    

    and then use this code on form1 again :

    Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
        If Form2.IsHandleCreated = True Then
            Form2.Close()
        End If
    End Sub
    

提交回复
热议问题