Monday, May 17, 2010

How to minimize window from closing C# windows application

To minimize the window when user closes the form, use the code below.

 private void Alerts_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {               
                e.Cancel = true;
        this.WindowState = FormWindowState.Minimized;

            }           
        }

Add To Google BookmarksStumble ThisFav This With TechnoratiAdd To Del.icio.usDigg ThisAdd To RedditTwit ThisAdd To FacebookAdd To Yahoo

Thursday, May 13, 2010

How to prevent form closing in C# windows application

In some cases you need to prevent closing the application/form when user clicks windows close button.

Use the following code snippet to do so

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

Add To Google BookmarksStumble ThisFav This With TechnoratiAdd To Del.icio.usDigg ThisAdd To RedditTwit ThisAdd To FacebookAdd To Yahoo