user198003
user198003

Reputation: 11151

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!

Upvotes: 0

Views: 641

Answers (2)

Haris Hasan
Haris Hasan

Reputation: 30097

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;
    }
}

Upvotes: 3

Mario
Mario

Reputation: 36487

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;
}

Upvotes: 4

Related Questions