The FormClosed event occurs after the form is closed. This is definitely not what you want.
The FormClosing event is more likely what you need.
The FormClosing event is fired when any button or menu link is clicked. Everything that tries to close the form will raise the FormClosing event.
Now, a more appropriate way to use FormClosingEventArgs would be the following in your FormClosing event handler method:
if(m_configControl.Modified) e.Cancel = true;
therefore, if configuration management is not changed, the form will continue to close. You want it to not close only if there are changes or unsaved changes.
EDIT 2 . After re-reading your question, see below my changes in the upper code:
if(m_configControl.Modified) if(DialogResult.OK == MessageBox.Show("Do you want to save changes?", "Changes detected") SaveChanges();
Then, the user is prompted for unsaved changes and saved only if the user clicked OK in the MessageBox. Please note that you will have to add the appropriate buttons to the additional parameters of the MessageBox.Show () method.
If you want to perform "Yes / No / Cancel when formatting", you will have to go as follows:
if(m_configControl.Modified) { DialogResult dr = MessageBox.Show(...); switch(dr) { case DialogResult.OK: SaveChanges(); break; case DialogResult.No: // Do nothing... break; case DialogResult.Cancel: e.Cancel = true; } }
So, when the user clicks βYesβ, your application will save the changes. When he clicks "No", the application will do nothing and continue to close. When the user wants to return to the application, Close will be closed.
EDIT 1 Take a look at my answer to This question , which seems completely what you want to accomplish.
Will marcouiller
source share