Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle FormClosing using MVP

Tags:

c#

mvp

winforms

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;
        }
    }
}
like image 800
smoak Avatar asked Oct 27 '25 16:10

smoak


1 Answers

I know this is old, but I felt this was a confusing thing to me at first as well. This is the way I handled this. Hopefully, someone stumbles along and finds this useful.

In the interface for the view.

public interface IMainScreenView
{
    event BoolHandler CloseView;

    bool IsDirty { get; set; }
    bool WillDiscardChanges();
}

Then in your presenter you subscribe your close function to the CloseView event.

public MainScreenPresenter(IView _view)
{
    _view.CloseView += Close;
}

private bool Close()
{
    bool close = true;

    if (_view.IsDirty)
        close = _view.WillDiscardChanges();

    return close;
}     

So now, in the view itself, you can subcribe to the FormClosing event as usual and make use of the CloseView event.

public MainForm()
{
    InitializeComponent();

    _presenter = new MainScreenPresenter(this);
    FormClosing += UxMainScreenFormClosing;
}

public bool WillDiscardChanges()
{
    return MessageBox.Show("Changes have been made without being saved. Would you like to continue?", "Exiting", MessageBoxButtons.YesNo) == DialogResult.Yes;
}

protected void UxMainScreenFormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = !CloseView();
}

For no reason other than completeness, I included the discard changes check as well. This keeps the view clean and the logic tucked away in the presenter. I hope it helps. :)

like image 115
yellow_nimbus Avatar answered Oct 29 '25 06:10

yellow_nimbus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!