This question already has answers here:
How do I prevent the app from terminating when I close the startup form?
(4 answers)
Closed 9 years ago.
I am trying to reset my main form so that I can reset all text boxes and variables easily. I have added a bool to my Progam.cs to enable the application to stay open while the form is closed and then re-opened. When I try to close it, the on_closing even fires twice. I'm not sure what to do to stop it happening, but I know it's got to be something simple.
Program.cs:
static class Program
{
public static bool KeepRunning { get; set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
KeepRunning = true;
while (KeepRunning)
{
KeepRunning = false;
Application.Run(new Form1());
}
}
}
Form1:
private void button1_Click(object sender, EventArgs e)
{
Program.KeepRunning = true;
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dialogResult = MessageBox.Show("You have unsaved work! Save before closing?", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
if (dialogResult == DialogResult.Yes)
{
e.Cancel = true;
MessageBox.Show("saving then closing");
Application.Exit();
}
if (dialogResult == DialogResult.No)
{
MessageBox.Show("closing");
Application.Exit();
}
if (dialogResult == DialogResult.Cancel)
{
e.Cancel = true;
MessageBox.Show("canceling");
}
}
Remove your Application.Exit(). Since you are already in the FormClosing Event Handler, the Application will exit if Program.KeepRunning is set to false.
This happens because you call Application.Exit(). Since, your form is not closed yet, if you try to close the application, that instruction will try to close the form fist which, in turn, will call the event handler a second time.
Also, I don't think you need Application.Exit() because that's your only form and the application will, therefore, close automatically (at least that's what happened in my VB6 old times!)
Related
Is there any way to run the application on the background even if the application/form is closed. All i can do now is just minimize it.
private void Form2_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Normal)
{
Hide();
WindowState = FormWindowState.Minimized;
}
else if (FormWindowState.Normal == this.WindowState)
{
Show();
}
}
If your application (your process) is closed, the notify icon will disappear. There is no way to avoid that.
So what you probably want is to keep your application running even if the user closes your Form. One way to achieve this is to actually not close the form but just hide it.
Therefor you need to subscribe to the Closing event of the Form:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Closing += Form1_Closing;
}
}
and implement the event handler like this:
void Form1_Closing(object sender, FormClosingEventArgs e)
{
Hide();
e.Cancel = true;
}
By setting e.Cancel = true you tell the form not to close. So you are just hiding it.
You'll need to add some code to your notify icon to reopen (Show()) the form again.
This question already has an answer here:
MessageBox on Form Closing
(1 answer)
Closed 8 years ago.
I wanted that when user closes the window in (Window Form Application, C#) by clicking (X) or pressing ESC key or by pressing ALT+F4, an alert will show i.e a Dialog(containing two buttons OK & CANCEL).
How to do?
You can find this by a simple search!
Handle Closing event of your form:
this.Closing += OnClosing; // For example put this in the constructor of your form
private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
{
string msg = "Do you want to close this?";
DialogResult result = MessageBox.Show(msg, "Close Confirmation",
MessageBoxButtons.YesNo/*Cancel*/, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
/* Do something */;
else if (result == DialogResult.No)
cancelEventArgs.Cancel = false;
}
you could do this at application level
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += Application_ApplicationExit;
AppDomain.CurrentDomain.ProcessExit += Application_ApplicationExit;
Application.Run(new Form1());
}
static void Application_ApplicationExit(object sender, EventArgs e)
{
//Do something
}
This question already has answers here:
C# Cancel Windows Shutdown
(3 answers)
How to detect Windows shutdown or logoff
(3 answers)
Closed 8 years ago.
I have a FormClosing method for my program to ask the user if he want to exit the program before closing the form. This works great, but I don't want the dialog to show up when the program closes because of a system shutdown / logout. How can I detect that the kill command is sent by the system and not by the user clicking the x of my form? Envrionment.HasShutdownStarted does not work.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Need to check for system shutdown here before the next if is activated
if (MessageBox.Show(...) == DialogResult.No)
{
e.Cancel = true;
this.Activate();
}
}
Try checking e.CloseReason, e.g.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.WindowsShutDown)
{
if (MessageBox.Show(...) == DialogResult.No)
{
e.Cancel = true;
this.Activate();
}
}
}
I have this event in a new Form:
private void CrawlLocaly_FormClosed(object sender, FormClosedEventArgs e)
{
}
Im not sure if to use Closed or Closing.
The reason is that i want to check if the user shut down the program for example by just closing the application from the taskbar.
If he did close it from the taskbar mouse right click then close then i want it to close all the program and not only this Form.
How can i do it ?
Application.Exit();
Will shut down your application.
Im not really sure if you can detect if he closed it via rightmouse menu. As far as I know you can only see the reasons provided in the FormClosedEventArgs. FormClosing will provide you with same reasons.
Use this event:
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
if(e.CloseReason == CloseReason.UserClosing)
{
//close forms
Application.Exit();
}
}
There is no way to check whether user closed your form by clicking 'X' or through TaskBar or any other way as the result of CloseReason will always be CloseReason.UserClosing
Well I Stick with such issue also.
In mine app I have 2 forms, #1 main, #2 - for settings - if user close it i want to know save settings or not. Also if settings are null - close app not only form, if user click button save - I want to close (hide) #2 form.
So where is my solution we set tag value to 1 if click button save, so we will know "who" try to close form:
Predefined:
btnSave.Tag = 0;
On save button click event:
btnSave.Tag = 1;
this.Hide();
its will trigger onclose event:
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
{
if (btnSave.Tag.ToString() == "0")
{
DialogResult dlg = MessageBox.Show("Do you want to exit without finished setup connection?", "Form", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dlg == DialogResult.No)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
this.Dispose();
Application.Exit();
}
}
else
{
this.Hide();
}
}
I am developing a application using windows forms. The project contains 3 forms: one login form which is the main form and two others which are child forms to the login form.
My problem is when want to close the total application by using Application.Exit() in form closing event my messagebox showing the dialog more than once.
1.This code in Login form i.e main form:
private void FrmLogIn_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult loginResult = MessageBox.Show("Do you want to close this application?","Close",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (loginResult == DialogResult.Yes)
{
Application.Exit();
}
}
2.AdminForm closing event which is child form to login form:
private void FrmAdmin_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult loginResult = MessageBox.Show("Do you want to close this application?","Close",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (loginResult == DialogResult.Yes)
{
Application.Exit();
}
}
3.Billoperations form closing event which is child form to login form:
private void FrmBillOperation_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult loginResult = MessageBox.Show("Do you want to close this application?","Close",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (loginResult == DialogResult.Yes)
{
Application.Exit();
}
}
When i click the close button in any form it will show MessageBox message only once. Please help me.
Make all FormClosing methods call a ApplicationShutdown function which handles this in a central place. You don't want to copy this code to every new form you create.
In this method you can check a boolean (watch for thread-safety) called for example IsShuttingDown. If it's already true, leave the method, otherwise you ask the question and start exiting.
The FormClosingEventArgs instance passed to the FormClosing event has a CloseReason property, which will be set to CloseReason.ApplicationExit when the Exit method of the Application class has been invoked: your handlers should check for this condition and if so then take no further action.
private void FrmLogIn_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.ApplicationExit)
return;
...
}
You can try with this code
FormCollection fc = Application.OpenForms;
if (fc!= null && fc.Count > 0)
{
for (int i = 1; i < fc.Count; i++)
{
if (fc!= null && fc.IsDisposed!= true)
{
fc.Dispose();
}
}
}
private void sh_interface_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
foreach (Form f in Application.OpenForms)
{
if (!f.IsDisposed)
f.Dispose();
}
}
else
{
e.Cancel = true;
this.Activate();
}
}
This will close all forms including the hidden forms and the main form from Application.Run(new something())...Also this method works when invoked in inherited classes while coded in template class Form Closing event.