Questions tagged [formclosing]

Form.FormClosing Event occurs Occurs before the form is closed.

The FormClosing event occurs as the form is being closed. When a form is closed, it is disposed, releasing all resources associated with the form. If you cancel this event, the form remains opened. To cancel the closure of a form, set the Cancel property of the FormClosingEventArgs passed to your event handler to true.

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. You can override the value assigned to the DialogResult property when the user clicks the Close button by setting the DialogResult property in an event handler for the FormClosing event of the form.

Note: The Closing event became obsolete in the .NET Framework version 2.0; so we use the FormClosing event instead.

Find the full documentation here.

187 questions
50
votes
6 answers

Awaiting Asynchronous function inside FormClosing Event

I'm having a problem where I cannot await an asynchronous function inside of the FormClosing event which will determine whether the form close should continue. I have created a simple example that prompts you to save unsaved changes if you close…
Hagelt18
  • 828
  • 1
  • 10
  • 21
21
votes
8 answers

How to close form

Ok, so a Windows Forms class, WindowSettings, and the form has a "Cancel"-button. When the user clicks the button, the dialog DialogSettingsCancel will pop-up up and ask the user if he is sure he wants to perform the action. The dialog has 2…
osvein
  • 625
  • 2
  • 10
  • 31
20
votes
5 answers

How do I prevent a form object from disposing on close?

I am using an MDIParent Form. When I close its child, the object of the child disposes. Is there a way to set child visibility to false instead of disposing?
Touseef Khan
  • 413
  • 5
  • 11
  • 26
19
votes
3 answers

Get the application closing event

How to get to the application closing event? I want to get application to save all unsaved operations or show the user a message "Are you sure about closing without saving all data?" something like this. If this will be helpful - the application…
Bulit
  • 975
  • 6
  • 15
  • 26
11
votes
4 answers

"e.Cancel " in formclosing Event

When using the FormClosing event, why does the code e.Cancel = true; work, but new CancelEventArgs().Cancel = true; does not work? private void Form1_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; new…
user635409
9
votes
2 answers

VB.NET: Abort FormClosing()

I have a code snippet that I want to run when the app is closing. So, I used FormCLosing event. But now i wanna place a confirmation message for exiting. Like, if the user clicks the Exit(X) button, there'll be a prompt, if he clicks NO, then the…
Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96
9
votes
1 answer

MessageBox on Form Closing

I'm use this code for question before closing the application, but it is not working correctly. My code is as below. private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult dlgresult = MessageBox.Show("Exit or no?", …
Federal09
  • 639
  • 4
  • 9
  • 25
6
votes
1 answer

c# WinForms form still closes after setting e.Cancel = true in Form_FormClosing event

This is the code in question: private void FormAccounting_FormClosing(object sender, FormClosingEventArgs e) { Properties.Settings.Default.FormAccountingLocation = this.Location; Properties.Settings.Default.Save(); if…
fenix
  • 215
  • 1
  • 3
  • 8
6
votes
4 answers

Window close events in a winforms application

I am looking to prompt the user to save data when they close a form window in a winforms application. I can't figure out how to trigger the prompt to the user, should they click the red box at the top right corner of the form. My application…
user279521
  • 4,779
  • 21
  • 78
  • 109
6
votes
6 answers

C# MDI Parent detect when MDI Child is closing?

I'm attempting to detect, on the MDI parent, when my MDI child form closes, and react accordingly. The MDI parent shouldn't do anything until the MDI child closes. Here is my code, I'm unsure as to what I'm doing wrong, but the form closed event…
CODe
  • 2,253
  • 6
  • 36
  • 65
5
votes
3 answers

Application.Exit() and FormClosing event in Vb.net

I have a single windows form application that is running in system tray icon.If the user press X button of the windows form a messagebox is displayed with Yes and No ( Yes ->close the form---No->keep the form running in system tray icon). I was…
Operagust
  • 137
  • 5
  • 6
  • 16
4
votes
1 answer

Firemonkey: How to detect when the OS (Android/iOS) is going to shutdown

I am dealing with the following issue with Firemonkey (Delphi 10.4): When the Android OS will shut down and my app is still running, it does not trigger the OnCloseQuery, OnClose, and nor the OnDestroy events. Is there a way to detect or intercept…
LUIS A. GAMA
  • 117
  • 6
4
votes
3 answers

How do prevent any Form from closing using alt + F4

This is not a duplicate of How to Disable Alt + F4 closing form?. Please read why. I have made a custom MessageBox under my main Form. And have set "Aight" button click listener as: private void Aight_buton_Click(object sender, EventArgs e) { …
Rishav
  • 3,818
  • 1
  • 31
  • 49
4
votes
4 answers

How can I prevent a user from closing my C# application?

How to make unclosed application in C#? I want to disable the 'X' button of the form and prevent the Windows Task Manager from closing it as well. I know that one way to prevent a form from closing is to handle the FormClosing event, but how do I…
Thiru G
  • 620
  • 2
  • 6
  • 17
4
votes
1 answer

Cancel form closing from some event

I want my formClosing event to cancel its closing operation if the SaveFileDialog, in my SaveAs Click event, is Cancel void exitToolStripMenuItem_Click (object sender, EventArgs e) { this.Close (); } void form1_FormClosing (object sender,…
Naveen Kumar V
  • 2,559
  • 2
  • 29
  • 43
1
2 3
12 13