How do I know that the user clicked the "X" or "Close" button?

In MSDN, I found CloseReason.UserClosing to find out that the user decided to close the form but I think it is the same for clicking the X button or clicking the close button. So how can I distinguish between the two in my code?

Thanks to everyone.

+76
c # winforms
Apr 21 '10 at 14:15
source share
8 answers

Assuming you are requesting WinForms, you can use the FormClosing () event . The FormClosing () event is fired at any time when the form closes.

To determine if user X clicked or your CloseButton, you can get it through the sender object. Try sending the sender as a Button control and maybe check, for example, its name "CloseButton".

 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (string.Equals((sender as Button).Name, @"CloseButton")) // Do something proper to CloseButton. else // Then assume that X has been clicked and act accordingly. } 

Otherwise, I never needed to distinguish whether X or CloseButton was pressed, because I wanted to do something specific in the FormClosing event, for example, close all MdiChildren before closing MDIContainerForm or checking events for unsaved changes. In these conditions, we do not need, in my opinion, to distinguish from both buttons.

Closing ALT + F4 also raises the FormClosing () event because it sends a message to the form that says close. You can cancel the event by setting

 FormClosingEventArgs.Cancel = true. 

In our example, this will mean

 e.Cancel = true. 

Note the difference between the FormClosing () and FormClosed () events.

FormClosing occurs when a form receives a message to close and checks to see if it has something to do before closing.

FormClosed occurs when the form is actually closed, therefore, after it is closed.

Does it help?

+75
Apr 21 '10 at 14:20
source share

The CloseReason enumeration that you found on MSDN is only for checking whether the user closed the application, or it was caused by a disconnection or closed by the task manager, etc.

You can do different actions, depending on the reason, for example:

 void Form_FormClosing(object sender, FormClosingEventArgs e) { if(e.CloseReason == CloseReason.UserClosing) // Prompt user to save his data if(e.CloseReason == CloseReason.WindowsShutDown) // Autosave and clear up ressources } 

But, as you already guessed, there is no difference between pressing the x button or right-clicking the taskbar and pressing "close" or pressing Alt F4 , etc. It all ends with CloseReason.UserClosing .

+72
Apr 21 '10 at 14:32
source share

The X button is registered as DialogResult.Cancel , so another parameter should evaluate DialogResult .

If you have several buttons in the form, you probably already associate different DialogResult with each, and this will give you the opportunity to tell the difference between each button.

(Example: btnSubmit.DialogResult = DialogResult.OK , btnClose.DialogResult = Dialogresult.Abort )

  public Form1() { InitializeComponent(); this.FormClosing += Form1_FormClosing; } /// <summary> /// Override the Close Form event /// Do something /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_FormClosing(Object sender, FormClosingEventArgs e) { //In case windows is trying to shut down, don't hold the process up if (e.CloseReason == CloseReason.WindowsShutDown) return; if (this.DialogResult == DialogResult.Cancel) { // Assume that X has been clicked and act accordingly. // Confirm user wants to close switch (MessageBox.Show(this, "Are you sure?", "Do you still want ... ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { //Stay on this form case DialogResult.No: e.Cancel = true; break; default: break; } } } 
+37
Dec 01 '11 at 17:31
source share

Determines when to close the application, if the form is closed (if your application is not attached to a specific form).

  private void MyForm_FormClosed(object sender, FormClosedEventArgs e) { if (Application.OpenForms.Count == 0) Application.Exit(); } 
+6
Oct 19 '12 at 19:33
source share

I always use the Form Close method in my applications, which catches alt + x from my exit button, alt + f4, or another form closing event. All my classes have a class name defined as Private string mstrClstitle= "grmRexcel" in this case, the Exit method, which calls the form closing method and the form closing method. I also have an instruction for the form closing method - this.FormClosing = My Form Closing Form Closing method name .

Code for this:

 namespace Rexcel_II { public partial class frmRexcel : Form { private string mstrClsTitle = "frmRexcel"; public frmRexcel() { InitializeComponent(); this.FormClosing += frmRexcel_FormClosing; } /// <summary> /// Handles the Button Exit Event executed by the Exit Button Click /// or Alt + x /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExit_Click(object sender, EventArgs e) { this.Close(); } /// <summary> /// Handles the Form Closing event /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void frmRexcel_FormClosing(object sender, FormClosingEventArgs e) { // ---- If windows is shutting down, // ---- I don't want to hold up the process if (e.CloseReason == CloseReason.WindowsShutDown) return; { // ---- Ok, Windows is not shutting down so // ---- either btnExit or Alt + x or Alt + f4 has been clicked or // ---- another form closing event was intiated // *) Confirm user wants to close the application switch (MessageBox.Show(this, "Are you sure you want to close the Application?", mstrClsTitle + ".frmRexcel_FormClosing", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { // ---- *) if No keep the application alive //---- *) else close the application case DialogResult.No: e.Cancel = true; break; default: break; } } } } } 
+3
May 14 '18 at 17:45
source share

You can try to add an event handler from the project as follows: Open the form in the design window, open the properties window or press F4, click the button of the event event panel to view the events in the Form object, find the FormClosing event in the Behavior group and double-click on it. Link: https://social.msdn.microsoft.com/Forums/vstudio/en-US/9bdee708-db4b-4e46-a99c-99726fa25cfb/how-do-i-add-formclosing-event?forum=csharpgeneral

+2
Aug 25 '16 at 15:32
source share
 if (this.DialogResult == DialogResult.Cancel) { } else { switch (e.CloseReason) { case CloseReason.UserClosing: e.Cancel = true; break; } } 

if the condition is met when the user clicks the "X" button or the close button on the form. Another can be used when the user press Alt + f4 for any other purpose.

+1
Jan 12 2018-12-12T00:
source share
 namespace Test { public partial class Member : Form { public Member() { InitializeComponent(); } private bool xClicked = true; private void btnClose_Click(object sender, EventArgs e) { xClicked = false; Close(); } private void Member_FormClosing(object sender, FormClosingEventArgs e) { if (xClicked) { // user click the X } else { // user click the close button } } } } 
0
Dec 01 '18 at 18:57
source share



All Articles