steve cook
steve cook

Reputation: 3214

Open dialog box prevents windows shutdown

I have a C# GUI app that shows a message using MessageBox.Show(Message);, however if the user fails to click on this, and then requests shutting down the PC, it blocks the shutdown. How do I prevent my open dialog box from blocking the shutdown?

Upvotes: 1

Views: 541

Answers (1)

Paul Keister
Paul Keister

Reputation: 13077

I'm assuming you're using WinForms since you didn't mention WPF. You can't use a MessageBox if you want to control closing behavior. You'll have to build your own screen to act as a message box and use the ShowDialog method to display it. Your screen can handle the FormClosing event to detect when Windows is shutting down:

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.WindowsShutDown)
        {
            //...
        }
    }

So you'll want to allow the screen to close in this case and perhaps take other action for other types of close signals. To prevent the screen from closing, set the Cancel flag on the FormClosingEventArgs parameter to true;

Upvotes: 3

Related Questions