How to make a ban on closing a user form?

    3 answers 3

    You can subscribe to the FormClosing event and, using the FormClosingEventArgs argument, cancel the closure of the form.

     private void MyForm_FormClosing(object sender, FormClosingEventArgs e) { //Отменяем закрытие формы e.Cancel = true; } 

      You can hide the form close button:

       private const int CP_NOCLOSE_BUTTON = 0x200; protected override CreateParams CreateParams { get { CreateParams myCp = base.CreateParams; myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ; return myCp; } } 
      • This can be done in a designer, no? - Qwertiy
      • one
        @Qwertiy, as far as I remember, in the designer you can only select the “Collapse” and “Expand” buttons. - Vlad

      Try to make a check in the event handler of the form Close and prohibit (or resolve) closing using e.Close = false or e.Close = true

      • 2
        It seems the next answer is more correct. And then the event is not that, and the property is the same. - Qwertiy