Here goes:
I have a view that looks like this:
public interface IAddressView
{
void Close();
bool IsDirty();
bool DoesUserWantToDiscardChanges();
}
I have a presenter that looks like this:
public class AddressViewPresenter
{
private IAddressView m_addressView;
private Address m_address;
public AddressViewPresenter(IAddressView addressView)
{
m_addressView = addressView;
m_address = new Address();
}
public void CancelButtonClicked()
{
bool canClose = true;
if (m_addressView.IsDirty())
{
canClose = m_addressView.DoesUserWantToDiscardChanges();
}
if (canClose)
{
m_addressView.Close();
}
}
public void SaveButtonClicked()
{
// saving logic goes here...removed for brevity
m_addressView.Close();
}
}
I then have a windows form that has a Cancel Button, a Save Button and all the controls for displaying an address. The cancel button runs:
m_addressPresenter.CancelButtonClicked();
which in turn checks if the view is dirty and prompts the user to discard any changes. This is great, it's what I want.
Now my question is how to achieve the same thing if the user closes the form without clicking Cancel (i.e. they clicked the "X" at the top right or even hit ALT+F4). I've tried handling the FormClosing event but I ended up duplicating some code and the pop up occurs twice if I click the cancel button. Here's what I have:
private void AddressForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.IsDirty())
{
if (!this.DoesUserWantToDiscardChanges())
{
e.Cancel = true;
}
}
}