Search code examples
c#titlebar

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!


Solution

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