Tim Huffam

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

  Home  |   Contact  |   Syndication    |   Login
  152 Posts | 0 Stories | 2310 Comments | 653 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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
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.

# re: How to hide a form instead of closing it - with C# .Net 4/1/2010 1:14 AM Abdur Rehman
It is a good site but have some need to improve.

# re: How to hide a form instead of closing it - with C# .Net 9/6/2010 1:48 AM akanksha mishra
thanks sir.this code is very helpful for me.

# running time application the error is occour 9/9/2010 12:55 AM Prathamesh Deshmukh
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


# re: How to hide a form instead of closing it - with C# .Net 9/15/2010 9:13 AM Vinzy
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?

# re: How to hide a form instead of closing it - with C# .Net 12/1/2010 3:45 AM Olga
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()
}

# re: How to hide a form instead of closing it - with C# .Net 4/29/2011 9:45 AM kiran
thanks lot

# re: How to hide a form instead of closing it - with C# .Net 6/18/2011 12:22 AM amal
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?

# re: How to hide a form instead of closing it - with C# .Net 10/9/2011 11:31 AM Mukund.Potdar
You r really brilliant and awesome to show this code

Thank you very much

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

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