This question already has answers here:
How can I prevent a user from closing my C# application?
(4 answers)
Closed 7 years ago.
I want to close a application when a button is pressed but I wanted to disable the close button (X button upper right).
I disabled the close button with this code:
protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = true;
}
But now when I try to close the program with this code it wont work.
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
Is there a way to close this program when the button is clicked?
In the event handler, check the CloseReason property of the FormClosingEventArgs:
This allows you to behave differently depending on how the close was initiated, so in the case of Application Exit (or Windows Shutdown) you can allow the form to close.
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.ApplicationExitCall
&& e.CloseReason != CloseReason.WindowsShutDown)
{
e.Cancel = true;
}
}
You're cancelling ALWAYS the closign of the form, so that's why it doesn't works.
Try this:
bool blockClosing = true;
protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = blockClosing;
}
private void button1_Click(object sender, EventArgs e)
{
blockClosing = false;
Application.Exit();
}
In this way, when you press your button it will allow tha pp to be closed.
FormClosingEventArgs has a Reason member that tells you what exactly is trying to close your form. Simply allow ApplicationExitCall through without canceling it.
Related
I've made a form closing event when X is pressed, but I also want the 'Exit' button to call this same method yet it draws me error every time I change stuff.
--- This code below is the form closing event ---
// if user pressed 'Exit' button or red cross
private void TempConverterForm1_FormClosing(object sender, FormClosingEventArgs e) {
DialogResult exitdialog = MessageBox.Show("Are you sure you want to quit?", "Quit?", MessageBoxButtons.YesNoCancel);
if (exitdialog == DialogResult.Yes) {
e.Cancel = false;
}
else {
e.Cancel = true;
}
}
--- This code below is the code I'm trying to solve ---
// if the 'exit' button is pressed
private void btn_Exit_Click(object sender, EventArgs e) {
TempConverterForm1_FormClosing(sender, (FormClosingEventArgs) e);
}
I've tried without FormClosingEventArgs first but on itself it says that EventArgs can't be converted to closing event. I put FormClosingEventArgs but now it tries to convert from MouseEventArgs to FormClosingEventArgs even though I relate to button click and not mouse click.
I tried to do research but the problem repeats and builds up with different error messages and I got lost and decided I need help with this.
Just do this.Close() in btn_Exit_Click. This will fire Form_Closing correctly with the right arguments, and your cancel will still work.
private void btn_Exit_Click(object sender, EventArgs e)
{
this.Close();
}
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();
}
}
}
Recently I had to edit my program code so that the form will close after creating a PDF. In FormClosing() there's a MessageBox.Show for closing or not, depending on the DialogResult. The problem is that when I try to Close(), it shows me the MessageBox, I need to close it without showing it. Thanks.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Exit?", "Exit", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}
private void btn_PdfCreate_CloseForm_Click(object sender, EventArgs e)
{
showPDf();
// close pdf but skip MessageBox
}
You can stop listening to the event like so
private void btn_PdfCreate_CloseForm_Click(object sender, EventArgs e)
{
this.FormClosing -= Form1_FormClosing
showPDf();
Close();
}
You can use the CloseReason property of the FormClosingEventArgs:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.ClosingReason == CloseReason.UserClosing && MessageBox.Show("Exit?", "Exit", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}
Use e.ClosingReason to find out if the formClosing event was fired by the user's attempt to close the form, or by something else.
for further reading, go to MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason(v=vs.110).aspx
You anyways want to close the form after pdf creation. So call Form's Dispose method just after pdf creation like below and no need of registering for the OnFormClosing event
private void btn_PdfCreate_CloseForm_Click(object sender, EventArgs e)
{
showPDf();
this.Dispose();
}
I overridden the FormClosing event to minimize to system tray when clicked. Here is my code:
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
notifyIcon.BalloonTipText = "Server minimized.";
notifyIcon.ShowBalloonTip(3000);
}
else
{
this.Close();
}
}
And I also set the notifyIcon's DoubleClick event as well, here is the code:
private void showWindow(object sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
I have two questions regarding this:
1) Now, when the upper-right "X" button is clicked, the application is minimized to the tray, but I can't close it (makes sense...). I wish to click right click on the icon in the system tray, and that it will open a menu with, let's say, these options: Restore, Maximize and Exit.
2) (This is may be related to me exiting the program with shift+f5 since I can't, for now, close my application because of the changes I mentioned).
When the application quits, after I minimzed it to the tray, the icon is left in the tray, until I pass over it with my mouse.
How can I fix it?
Just add a variable that indicates that the close was requested by the context menu. Say:
private bool CloseRequested;
private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
CloseRequested = true;
this.Close();
}
protected override void OnFormClosing(FormClosingEventArgs e) {
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.UserClosing && !CloseRequested) {
e.Cancel = true;
this.Hide();
}
}
Be sure to not call Close() in the FormClosing event handler, that can cause trouble when the Application class iterates the OpenForms collection. The possible reason that you are left with the ghost icon. No need to help.
I have a short questions and even after a while of searching through the web I have not found the answer.
I have two windows in a WPF application. One window should just be hidden when the user closes it. When the main window is closed the complete application shall close.
I used
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
Hide();
}
inside the class of the second window and hoped it would just catch its Close() event but unfortunately it catches all Close() events.
How can I separate between the windows to handle the events independently?
Regards
Larimow
Use the sender parameter, that's what it's there for:
window1.Closing += Window_Closing;
window2.Closing += Window_Closing;
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
var w = (Window)sender;
// Simple ref test to illustrate, but you can use anything else you want instead
if (w == window1) {
e.Cancel = true;
w.Hide();
}
}