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

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

    If it is a child form in an MDI application, you could trap the click in the parent form, otherwise the solution will be messy.

    I am not convinced what you suggest represents intuitive UI behaviour anyway. Are you sure that is the best design?

    0 讨论(0)
  • 2021-01-12 16:54

    this is simple :

    private void button1_Click(object sender, EventArgs e)
        {
            Form f = new Form();
            f.LostFocus +=new EventHandler(f_LostFocus);
            f.Show();
        }
    
        void f_LostFocus(object sender, EventArgs e)
        {
            Form f = sender as Form;
            f.Close();
            f.Dispose();
        }
    
    0 讨论(0)
提交回复
热议问题