Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 227211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:34:46+00:00 2026-05-11T19:34:46+00:00

Here goes: I have a view that looks like this: public interface IAddressView {

  • 0

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;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:34:46+00:00Added an answer on May 11, 2026 at 7:34 pm

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like a fool, but here goes: public interface IHasErrorController{ ErrorController ErrorController {
This question might be kind of elementary, but here goes: I have a SQL
I'm not sure if this is even possible but here goes: Currently I have
Similar questions have been asked, but nothing exactly like mine, so here goes. We
This is sort of SQL newbie question, I think, but here goes. I have
This may seem like a stupid question, so here goes: Other than parsing the
I have a model like this: public PurchaseOrder { [Required] [StringLength(15)] public virtual string
I'll try to explain my problem the best I can, here goes: I have
Probably a long question for a simple solution, but here goes... I have a
First post, so here goes. I'm writing a script that does intelligent search and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.