Wed 21 Jan 2009

In windows form, if you don't want user to close the form by pressing Alt+F4 or Cross (x) button accidently and without saving existing form status, you may need to capture the form close event.

In the FormClosing event of form, FormClosingEventArg has a property called CloseReason. This is very usefull because if you want the Form to stop closing if the closing reason was CloseReason.UserClosing or some thing else.

You can do e.Cancel = true to prevent form being closed, then do something in your code and finally close the form programatically.

e.g.


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
                e.Cancel = true;

                //Check the status of form, Save form status if required or other logic

                this.Close();
        }

 

 

Tags: ,
Comments here


Add Comment Post comment

 
 
 
   Country flag

Loading