tags:

views:

157

answers:

1

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;
        }
    }
}
A: 

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:

public bool ViewRequestingClose()
{
    bool canClose = true;
    if (m_addressView.IsDirty())
    {
     canClose = m_addressView.DoesUserWantToDiscardChanges();
    }
    return canClose;
}

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.

private void AddressForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if(!m_addressPresenter.ViewRequestingClose())
     e.Cancel = true;
}

private void CancelButton_Click(object sender, FormClosingEventArgs e)
{
    if(m_addressPresenter.ViewRequestingClose())
     this.Close();
}
Lee
That does make more sense to have it that way. However, I am still experiencing the problem where it asks if you want to discard your changes two times when you click the cancel button.
smoak
You are getting the double prompt because when the Cancel is confirmed it will Close the form hit the same logic as when just attempting to close the form. You can use a bool to track if the closing is from a Alt+F4/"x" click or a Cancel click and only prompt the first time through.
Lee Roth
Yeah. I was just hoping to be able to do it differently. It works the way you describe if I add in a bool to track if the close is from the cancel button. I guess it feels a bit "cludgy" to use a bool that way. I suppose if it works, it works. Thanks.
smoak