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) {
Oliver Oct 26 '09 at 10:01 2009-10-26 10:01
source share