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;
}
}
}
The main problem you have is that the view contains logic which the presenter is responsible for, so I would change CancelButtonClicked method on the presenter to something like this:
the return value indicates whether the view should continue closing.
The two event handlers in the view for the cancel button click and form closing events will then query the presenter to see if they should continue closing or not e.g.