In the closing event, E. Cancle is set to True, Windows cannot shut down and restart the system workaround

Source: Internet
Author: User

Recently in the design of a WinForm program encountered a bug, that is, the From1 form is set in the Shutdown event e.cancle set to True, causing the system to shut down the restart, Windows7 and Windows XP.

What I am designing here is when the user clicks on the fork of the form, does not close the form, but minimizes the form, but when the system restarts, it is found irrelevant shutdown restart, the solution here is to determine the Closereason enumeration value, to figure out whether the user is shut down or the system caused by the form. The key code is as follows:

Private void Form1_formclosing (object sender, FormClosingEventArgs e) {            //When the user taps the fork, if it is not caused by the system shutdown, then minimize the form             if (e.closereason! = Closereason.windowsshutdown)            {                true;                this. WindowState = formwindowstate.minimized;            } Else            {                false;            }}

In the menu, another write code implements the form close exit.

Private void Exit Toolstripmenuitem_click (objectfalse;  this. Close (); this. Dispose (); Environment.exit (0);}

This allows you to minimize your form when you click the fork, without interrupting the system's shutdown restart.

In the closing event, E. Cancle is set to True, Windows cannot shut down and restart the system workaround

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.