Finding a reason for closing a form

How can I determine how the window shape closes? For example, how do you know if the user clicked the button that closes the form, or if the user clicks the "X" button in the upper right corner? Thank.

Update:

I forgot to note that the button calls the Application.Exit () method.

+25
c # winforms
Oct 26 '09 at 8:59
source share
3 answers

As the Bashohands and Dmitry Matveev already mentioned, the solution should be FormClosingEventArgs. But, as Dmitry said, this will not matter between your button and the X in the upper right corner.

To distinguish between these two parameters, you can add the ExitButtonClicked boolean property to your form and set it to true in the Click-Event button right before calling Application.Exit ().

Now you can set this property in the FormClosing event and distinguish between these two parameters in the case of UserClosing.

Example:

public bool UserClosing { get; set; } public FormMain() { InitializeComponent(); UserClosing = false; this.buttonExit.Click += new EventHandler(buttonExit_Click); this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); } void buttonExit_Click(object sender, EventArgs e) { UserClosing = true; this.Close(); } void Form1_FormClosing(object sender, FormClosingEventArgs e) { switch (e.CloseReason) { case CloseReason.ApplicationExitCall: break; case CloseReason.FormOwnerClosing: break; case CloseReason.MdiFormClosing: break; case CloseReason.None: break; case CloseReason.TaskManagerClosing: break; case CloseReason.UserClosing: if (UserClosing) { //what should happen if the user hitted the button? } else { //what should happen if the user hitted the x in the upper right corner? } break; case CloseReason.WindowsShutDown: break; default: break; } // Set it back to false, just for the case e.Cancel was set to true // and the closing was aborted. UserClosing = false; } 
+35
Oct 26 '09 at 10:01
source share

You can check the CloseReason property of FormClosingEventArgs in the FormClosing event handler to check some of the possible cases. However, the cases you describe will be indistinguishable if you use this property. You will need to write additional code in the event handler of the click of your “close” button to save some information that will be checked in the FormClosing event handler to distinguish between these cases.

+4
Oct 26 '09 at 9:10
source share

You need to add a listener to Even FormClosing, which sends a CloseReason type property to the args event, which is one of these values

  // Summary: // Specifies the reason that a form was closed. public enum CloseReason { // Summary: // The cause of the closure was not defined or could not be determined. None = 0, // // Summary: // The operating system is closing all applications before shutting down. WindowsShutDown = 1, // // Summary: // The parent form of this multiple document interface (MDI) form is closing. MdiFormClosing = 2, // // Summary: // The user is closing the form through the user interface (UI), for example // by clicking the Close button on the form window, selecting Close from the // window control menu, or pressing ALT+F4. UserClosing = 3, // // Summary: // The Microsoft Windows Task Manager is closing the application. TaskManagerClosing = 4, // // Summary: // The owner form is closing. FormOwnerClosing = 5, // // Summary: // The System.Windows.Forms.Application.Exit() method of the System.Windows.Forms.Application // class was invoked. ApplicationExitCall = 6, } 
0
Oct 26 '09 at 9:10
source share



All Articles