954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Hide a form when clicking on the X - Button

Hey guys,

I want my form to hide instead to close by clicking on the red X button. Because when it's closed the form is deleted.
I searched a lot to find a good solution, but nothing works.
Many people wrote that following code:

private void Form1_OnClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }


would solve the problem. I tried it. My Form is not hiding. I don't know what I did wrong.
I would be very happy if you could help me.

Thanks :)

Tortura
Newbie Poster
15 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

You forgot to manually bind the form event to that method.. is it being fired?

add this.FormClosing += new FormClosingEventHandler(Form1_OnClosing);

in your form load method

jfarrugia
Junior Poster
113 posts since May 2011
Reputation Points: 17
Solved Threads: 17
 

It works, you are a genius :)

I'm a newbie therefore I don't know such things.
Thank you so much :)

Tortura
Newbie Poster
15 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

lol,
he is not a genious. He only knows more.

jfarrugia btw, next time what is the code, you have to put it into code brackets!
I hope you understand (forum rules).

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: