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

前端 未结 8 2019
孤独总比滥情好
孤独总比滥情好 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:31

    You should use Deactivate:
    I have a form called Form2.
    In the Deactivate section of the Properties window.
    You declare the name Form2_Deactivate.
    In the Form2.cs file:

    private void Form2_Deactivate(object sender, EventArgs e)
    {
        this.Close();
    }
    

提交回复
热议问题