Search code examples
c#winformsformclosing

Is it possible to disable the "close" button while still being able to close from elsewhere?


I have a winforms application where I want the close button in the top-right corner of the program to instead minimize the program.

I have been able to achieve this by using the form's FormClosing event like this:

this.Hide();
e.Cancel = true;

But this unfortunately also stops any other close buttons I place on the form.

Is there a way to only stop the default button in the top-right but still be able to close the form elsewhere?


Solution

  • This is a simple boolean example:

    bool ExitApplication = false;
    
    private void Form1_FormClosing(Object sender, FormClosingEventArgs e) 
    {
        switch(ExitApplication)
        {
          case false:
          this.Hide();
          e.Cancel = true;
          break;
    
          case true:
          break;
        }
    }
    

    So when you want to close your application just set ExitApplication to true.