Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make close button hide instead of closing [duplicate]

Tags:

c#

.net

winforms

How can I make the close button on a form effectively act as a 'Hide' button?

Is there a way to abort the FormClosing event?

like image 722
adamwtiko Avatar asked May 19 '10 12:05

adamwtiko


People also ask

How do I disable the close button?

Answer: To disable the Close button on an Access form, open the form in Design view. Under the View menu, select Properties. When the Properties window appears, set the "Close Button" property to No.

How do I disable the close button in Winforms?

the first one is to set the border style to "none" in the properties menu, however this will completely remove all buttons and the "windows header thingy" in the top on the window. the second method is to set the controlBox property to false.

How do I remove the Close button from a Jpanel?

You can't remove close Button from JFrames, but you can disable by calling setDefaultCloseOperation(WindowConstants. DO_NOTHING_ON_CLOSE) on that JFrame.


2 Answers

You could just capture the FormClosing event and stop the default action, then instead of closing the form just hide it:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    this.Hide();
}
like image 178
djdd87 Avatar answered Oct 21 '22 22:10

djdd87


It should be pretty straight forward:

To cancel the closure of a form, set the Cancel property of the FormClosingEventArgs passed to your event handler to true.

Concerning part two of the question with hiding the window, hide to taskbar or hide to notification area?

like image 42
cyberzed Avatar answered Oct 21 '22 22:10

cyberzed