Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel Application Exit

Tags:

c#

winforms

My application is closing when I press Alt + F4. How I will do to have a MessageBox to show first for confirmation before exiting and if No is response the applcation will not continue to close?

like image 870
Karlx Swanovski Avatar asked Mar 24 '26 12:03

Karlx Swanovski


1 Answers

In addition to the answers already posted here, don't be the dork that hangs the complete system:

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

        // other logic with Messagebox
        ...
    }
like image 81
Henk Holterman Avatar answered Mar 26 '26 01:03

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!