In MSDN I found CloseReason.UserClosing
to know that the user had decided to close the form
but I guess it is the same for both clicking the X button or clickin
I also had to register the closing function inside the form's "InitializeComponent()" method:
private void InitializeComponent() {
// ...
this.FormClosing += FrmMain_FormClosing;
// ...
}
My "FormClosing" function looks similar to the given answer (https://stackoverflow.com/a/2683846/3323790):
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing){
MessageBox.Show("Closed by User", "UserClosing");
}
if (e.CloseReason == CloseReason.WindowsShutDown){
MessageBox.Show("Closed by Windows shutdown", "WindowsShutDown");
}
}
One more thing to mention: There is also a "FormClosed" function which occurs after "FormClosing". To use this function, register it as shown below:
this.FormClosed += MainPage_FormClosed;
private void MainPage_FormClosing(object sender, FormClosingEventArgs e)
{
// your code after the form is closed
}