Geeks With Blogs

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

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 C# .NET | Back to top


Comments on this post: How to hide a form instead of closing it - with C# .Net

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

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

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
hiding form using c#.net
Left by fahem on Feb 28, 2008 3:57 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
this is not allow windows to shutdown or restart if application is running.
Left by Jibran on Apr 03, 2008 10:57 PM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
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.
Left by Arun Pandiyarajan on Aug 15, 2008 12:30 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
"
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 ;)
Left by Javi on Sep 17, 2008 11:38 PM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
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.
Left by noobie2 on Jan 17, 2009 8:18 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
how to close a form with a button in C#???
Left by Brem on Mar 25, 2009 8:27 PM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
do-

private void button1_Click(object sender, EventArgs e)
{
this.close();
}
Left by Hugo on Apr 10, 2009 9:37 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
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!
Left by Hugo on Apr 10, 2009 9:39 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
It worked for me when I put the this.Hide() with a control variable on the OnPaint event.
Left by Suru on May 01, 2009 2:52 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
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!
Left by Sam on Jul 31, 2009 12:29 PM

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

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
how to reopen a form alredy in Hided form in c#.Net
Left by kiran on Nov 26, 2009 12:24 AM

# re: How to hide a form instead of closing it when another form is loded - with C# .Net
Requesting Gravatar...
plz give an idea
Left by Rajesh on Dec 07, 2009 1:53 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
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.
Left by cristian on Jan 24, 2010 3:25 PM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
It is a good site but have some need to improve.
Left by Abdur Rehman on Apr 01, 2010 1:14 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
thanks sir.this code is very helpful for me.
Left by akanksha mishra on Sep 06, 2010 1:48 AM

# running time application the error is occour
Requesting Gravatar...
when m run the application then following error is occour plz regards me the error is as follows:-

Error 1 Unable to copy file "obj\Debug\Hide test.exe" to "bin\Debug\Hide test.exe". The process cannot access the file 'bin\Debug\Hide test.exe' because it is being used by another process. Hide test
Left by Prathamesh Deshmukh on Sep 09, 2010 12:55 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
On rare occasions my window panels load event dont get fired. I do some UI modification after I hide the window. Could this be the reason for the failure?
Left by Vinzy on Sep 15, 2010 9:13 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
There is the full solution for hide onClosing event:
OnClosing(...)
{
base.OnClosing(e);
typeof(Window).GetField("_isClosing", BindingFlags.Instance |BindingFlags.NonPublic).SetValue(this, false);
e.Cancel = true;
this.Hide()
}
Left by Olga on Dec 01, 2010 3:45 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
thanks lot
Left by kiran on Apr 29, 2011 9:45 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
if (txtuser.Text == "admin")
{
if (txtpassword.Text == "1234")
{
Form1 temp = new Form1();
temp.Show;
temp.Hide;
Form2 asd = new Form2();
asd.Show();

}
else
{
MessageBox.Show("wrong password");
}
}
else
{
errorProvider1.Clear();
errorProvider1.SetError(txtuser, "wrong user name");
return;
}
i have done the above,but its not working.can u solve the problem?
Left by amal on Jun 18, 2011 12:22 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
You r really brilliant and awesome to show this code

Thank you very much
Left by Mukund.Potdar on Oct 09, 2011 11:31 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
if(txtuser.text=="admin" && txtpassword.text=="1234")
{
form2 f = new form2();
f.show();
this.hide();
}
else
{
messagebox.show("Invalid userid and password");
}
Left by Ranjan on Jan 25, 2012 7:13 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
in visual studio 2010 i have written this.hide();
and this.close(); also but none of dhem are working.....??????
can some one help me
Left by lekha on Mar 01, 2012 12:29 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
I want to hide form1 to show form2 ,,then KICK back button to close form2 to back form1..... how can i do it ....

i have use the property Dialogresult = OK of button in form2

Left by HENRYMK on Mar 14, 2012 10:39 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
how to get back the hidden form
Left by Hemanth on Jun 23, 2012 4:08 AM

# re: How to hide a form instead of closing it - with C# .Net
Requesting Gravatar...
hi! thanks for everything!
my problem no is that I hide the form and go to an other form, and want to come back to the hidden form.
How can I show it back?
Its like a back button

thanks
Left by iair on Aug 27, 2012 12:04 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Huffam | Powered by: GeeksWithBlogs.net