简体   繁体   中英

Don't want to exit program when user click on Close button in titlebar of C# app

Working on development of C# application.

I want to keep Close button on top right in title bar, but if end users click on it, all I want is that he gets info window that he can not close application until some other proper button.

Is it possible to achieve?

Tnx in adv!

Add an event handler to the OnClosing event of the form. The event argument contains an element Cancel . Set it to true and it won't close.

Essentially something like this:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = !stuffdone;
}

You can handle the FormClosing event event handler like this

private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
    //there can be other reasons for form close make sure X button is clicked
    // if close button clicked
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM