Search code examples
c#.netwinformsmdiformclosing

How to restrict user from closing the Winform c#


I know this is very simple question, but I am looking for neat and clean suggestion. My application is an MDI application. Created a base form which is being used in the whole applicaiton. There are couple of forms which we dont want user to close so to avoid human mistakes we have planned to not allow the user to close those forms.

I have tried to setting e.cancel = true in form closing event, but it seems its not working can somebody give me some suggestions please?

Edit:

Private void FrmTask_FormClosing(object sender, FormClosingEventArgs e)
        {         
                e.Cancel = true;
        }

The problem is that when I am using this code, none of the form in my MDI application are closing, even the main MDI Parent form.


Solution

  • You could also not show the close button:

    http://blogs.msdn.com/b/atosah/archive/2007/05/18/disable-close-x-button-in-winforms-using-c.aspx

    Cody Gray provided a better link in the comments that also disallows the Alt-F4 close:

    https://stackoverflow.com/a/4655948/366904