Tim Huffam

Dotting the I and crossing the T of I.T.

  Home  |   Contact  |   Syndication    |   Login
  140 Posts | 0 Stories | 1468 Comments | 659 Trackbacks

News

Archives

Post Categories

Interesting Blogs/Links

Doing some winforms dev for a change - and had a need to hide a form, instead of closing it, when the user clicked the standard windows close form button (little cross [x] icon on the top right corner of the form).

The solution is to create a handler for the FormClosing event of the form, then force the form to be hidden, then, the important part, cancel the close event...

Eg:

// Use this event handler for the FormClosing event.

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
  this.Hide();
  e.Cancel = true; // this cancels the close event.
}

Tim

posted on Thursday, September 13, 2007 4:37 AM

Feedback

# re: How to hide a form instead of closing it - with C# .Net 11/17/2007 9:07 AM Niklas
Love you! thanks, belive it or not but this was hard to find some info on.

# re: How to hide a form instead of closing it - with C# .Net 12/24/2007 12:04 PM Rita
lets say that i wan to close the sesion, it wouldn't let me , how can avoid that ??

# re: How to hide a form instead of closing it - with C# .Net 2/28/2008 3:57 AM fahem
hiding form using c#.net

# re: How to hide a form instead of closing it - with C# .Net 4/3/2008 10:57 PM Jibran
this is not allow windows to shutdown or restart if application is running.

# re: How to hide a form instead of closing it - with C# .Net 8/15/2008 12:30 AM Arun Pandiyarajan
thank you for this valuable tips,
i have another problem.

this.hide();// is working but
form1 n = new form1();
n.hide();// is not working can you explain it, please.

# re: How to hide a form instead of closing it - with C# .Net 9/17/2008 11:38 PM Javi
"
thank you for this valuable tips,
i have another problem.

this.hide();// is working but
form1 n = new form1();
n.hide();// is not working can you explain it, please.
"

when u create a new form, if you don't show it, then u can't hide it =).... if u put n.show();
and then (after a while) put n.hide(); u'll see it works ;)

# re: How to hide a form instead of closing it - with C# .Net 1/17/2009 8:18 AM noobie2
Make this mod to allow the owner to close the window:

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.FormOwnerClosing)
{
Hide();
e.Cancel = true; // this cancels the close event.
}
}

Then, in the owner's Form Closing, manually close the form. Application should exit then.


# re: How to hide a form instead of closing it - with C# .Net 3/25/2009 8:27 PM Brem
how to close a form with a button in C#???

# re: How to hide a form instead of closing it - with C# .Net 4/10/2009 9:37 AM Hugo
do-

private void button1_Click(object sender, EventArgs e)
{
this.close();
}

# re: How to hide a form instead of closing it - with C# .Net 4/10/2009 9:39 AM Hugo
Does anyone know how to make a log in screen using Windows forms and if they input the wrong info make the messege box say "Incorrect login info", i can make the login screen, just not the messege box, please help!

# re: How to hide a form instead of closing it - with C# .Net 5/1/2009 2:52 AM Suru
It worked for me when I put the this.Hide() with a control variable on the OnPaint event.

# re: How to hide a form instead of closing it - with C# .Net 7/31/2009 12:29 PM Sam
The code the make a messagebox is:
messagebox("INSERT TEXT HERE");

Just put that code into the correct part, and then you can change the text and voila!

# re: How to minimize a form instead of closing it - with C# .Net 11/19/2009 11:27 PM kalyan
sir can you help for this coding to minimize the window

# re: How to hide a form instead of closing it - with C# .Net 11/26/2009 12:24 AM kiran
how to reopen a form alredy in Hided form in c#.Net

# re: How to hide a form instead of closing it when another form is loded - with C# .Net 12/7/2009 1:53 AM Rajesh
plz give an idea

# re: How to hide a form instead of closing it - with C# .Net 1/24/2010 3:25 PM cristian
muchas gracias. estoy desarrollando una aplicacion en c# y para optimizarlo tuve que utilizar el patron singleton en algunos formularios, pero al cerrarlos me generaba un problema por que era eliminado de memoria, a causa de esto tu solucion me cayo como anillo al dedo.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: