Hide form instead of closing when close button clicked
Like so:
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
(via Tim Huffman)
How to hide Form instead of closing it
Use FormClosing
instead of FormClosed
. There you can do e.Cancel = true;
to achieve what you need. The problem is that the form is already Closed by the time FormClosed
event occurs, so Hide()
won't do any good and you won't be able to use this object in the future, if you try it with FormClosed
event.
How to Clicking X in Form, will not close it but hide it
Just implement the FormClosing event. Cancel the close and hide the form unless it was triggered by the notify icon's context menu. For example:
Private CloseAllowed As Boolean
Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
If Not CloseAllowed And e.CloseReason = CloseReason.UserClosing Then
Me.Hide()
e.Cancel = True
End If
MyBase.OnFormClosing(e)
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
CloseAllowed = True
Me.Close()
End Sub
Hiding or Closing a Windows Form on C#
Which is the best for performance & memory?
"Best" isn't a question nor a SMART requirement.
Of course if you just hide the form, it and its contents will stay in memory. This means your application uses more memory, but on the other hand, when you need to show the form again, you won't have to load the entries from the database again - making it appear faster.
If it's a form you really frequently need, I'd just hide and re-show it. See Hide form instead of closing when close button clicked how to do this.
Make close button hide instead of closing
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();
}
How to hide only the Close (x) button?
You can't hide it, but you can disable it by overriding the CreateParams property of the form.
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ;
return myCp;
}
}
Source: http://www.codeproject.com/KB/cs/DisableClose.aspx
Hiding a form without calling FormClosing method
I changed my program so that it is started as below:
MainForm mainForm = new MainForm();
mainForm.Show();
Application.Run();
Instead of:
Application.Run(new MainForm());
In each of the forms I have added a FormClosing event which checks to see if the user has opted to close the application. If this is the case a prompt is shown to the user to ask for their confirmation:
private void ImageSelect_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
if (DialogResult.No == MessageBox.Show("Are you sure you wish to exit?", "Exit Confirmation", MessageBoxButtons.YesNo))
e.Cancel = true;
else { Application.Exit(); }
}
}
The application now can be closed from any form in the application.
How to hide main form rather than closing it?
When the user closes a window, it receives a WM_CLOSE
message, which triggers TForm
to call its Close()
method on itself. Calling Close()
on the project's MainForm
always terminates the app, as this is hard-coded behavior in TCustomForm.Close()
:
procedure TCustomForm.Close;
var
CloseAction: TCloseAction;
begin
if fsModal in FFormState then
ModalResult := mrCancel
else
if CloseQuery then
begin
if FormStyle = fsMDIChild then
if biMinimize in BorderIcons then
CloseAction := caMinimize else
CloseAction := caNone
else
CloseAction := caHide;
DoClose(CloseAction);
if CloseAction <> caNone then
if Application.MainForm = Self then Application.Terminate // <-- HERE
else if CloseAction = caHide then Hide
else if CloseAction = caMinimize then WindowState := wsMinimized
else Release;
end;
end;
Only secondary TForm
objects respect the output of the OnClose
handler.
To do what you are asking for, you can either:
handle
WM_CLOSE
directly and skipClose()
.private
procedure WMClose(var Message: TMessage); message WM_CLOSE;
procedure TForm1.WMClose(var Message: TMessage);
begin
Hide;
// DO NOT call inherited ...
end;have your MainForm's
OnClose
handler callHide()
directly and returncaNone
:procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Hide;
Action := caNone;
end;
How do I hide a window form when x is clicked and CLose through system tray ToolStripMenuItem
Usually when I had to do this, I kept a statusflag to remember the call came from somewhere else to close me. So then in my closing handler I could check if I needed to close or hide...
bool bFormCloseRequested = false: //member of my Form
void MyCloseClick_Handler{object sender, eventArgs e)
{
if(!bFormCloseRequested)
{
e.Cancel = true;
this.hide();
}
}
Two different ways of handling the form Close event
A simple boolean flag should do the trick:
private bool saveClicked = false;
private void btnSave_click(object sender, EventArgs e)
{
saveClicked = true;
}
private void EmailNewsletter_FormClosing(object sender, FormClosingEventArgs e)
{
if(saveClicked)
return;
DialogResult dr = MsgBox.Show("Are you sure you want to dimiss this newsletter?", "Dismiss Newsletter", MsgBox.Buttons.YesNo, MsgBox.Icon.Question);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
this.Newsletter = null;
}
else
{
e.Cancel = true;
}
}
Related Topics
How to Detect Which .Net Runtime Is Being Used (Ms VS. Mono)
What's the Difference Between Double Quotes and Single Quote in C#
C# - Making All Derived Classes Call the Base Class Constructor
Generating a Random Decimal in C#
Adding Elements to an Xml File in C#
How Is the Boxing/Unboxing Behavior of Nullable<T> Possible
Why Does Guid.Tobytearray() Order the Bytes the Way It Does
Why Does This Simple .Net Console App Have So Many Threads
Simple Way to Display Row Numbers on Wpf Datagrid
Post String to ASP.NET Web API Application - Returns Null
Impersonate User in Windows Service
Differencebetween .Equals and ==
Reversible Shuffle Algorithm Using a Key
Programmatic Way to Get All the Available Languages (In Satellite Assemblies)