Détecter le motif de la fermeture du formulaire

Comment puis-je détecter comment un formulaire Windows est fermé? Par exemple, comment savoir si l’utilisateur a cliqué sur un bouton qui ferme le formulaire ou s’il clique sur le “X” en haut à droite? Je vous remercie.

Mettre à jour:

J’ai oublié de mentionner que le bouton appelle la méthode Application.Exit ().

Comme Bashmohandes et Dmisortingy Matveev l’ont déjà mentionné, la solution devrait être le FormClosingEventArgs. Mais comme Dmisortingy l’a également dit, cela ne ferait aucune différence entre votre bouton et le X situé dans le coin supérieur droit.

Pour distinguer ces deux options, vous pouvez append une propriété booléenne ExitButtonClicked à votre formulaire et lui atsortingbuer la valeur true dans le bouton Click-Event juste avant d’appeler Application.Exit ().

Vous pouvez maintenant demander cette propriété dans l’événement FormClosing et faire la distinction entre ces deux options dans le cas UserClosing .

Exemple:

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; } 

Vous pouvez vérifier la propriété CloseReason de FormClosingEventArgs dans le gestionnaire d’événements FormClosing pour vérifier certains des cas possibles. Cependant, les cas décrits par vous seront indiscernables si vous utilisez uniquement cette propriété. Vous devrez écrire du code supplémentaire dans le gestionnaire d’événements click de votre bouton “fermer” pour stocker des informations qui seront vérifiées dans le gestionnaire d’événements FormClosing pour faire la distinction entre ces cas.

Vous devez append un écouteur à Even FormClosing, qui envoie à l’événement args une propriété de type CloseReason qui est l’une de ces valeurs.

  // 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's 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, }