0
votes

I've come into a problem with trying to display user controls on buttons clicks like from a menu while still following the MVP design pattern. All I need to do is bring the user control to the front when a button is clicked but where should that be handled? I was thinking of having the user control call "this.BringToFront()" on button click but I believe that would break the passive view. I also tried this below using events:

View interfaced by ItipManager

public partial class Form1 : Form, ITipManager
    {
        public event EventHandler homeButtonClicked;
        public event EventHandler addTipButtonClicked;
        public event EventHandler transactionButtonClicked;
        public event EventHandler analysisButtonClicked;

        public Form1()
        {
            InitializeComponent();
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            EventHandler handler = addTipButtonClicked;
            handler?.Invoke(this, e);
        }

TipManagerPresenter, it's only jobs is to handle the button clicks

class TipManagerPresenter
    {
        private ITipManager tipManagerView;

        private AddTip addTipControl;
        private Home homeControl;

        public TipManagerPresenter(ITipManager tipManagerView, AddTip addTipControl, Home homeControl)
        {
            this.tipManagerView = tipManagerView;
            this.addTipControl = addTipControl;
            this.homeControl = homeControl;

            tipManagerView.addTipButtonClicked += new EventHandler(OnAddTipButtonClicked);
            tipManagerView.homeButtonClicked += new EventHandler(OnHomeButtonClicked);
        }

        public void OnAddTipButtonClicked(object sender, EventArgs e)
        {
            addTipControl.BringToFront();
        }

        public void OnHomeButtonClicked(object sender, EventArgs e)
        {
            homeControl.BringToFront();
        }

    }

I believe the above code also breaks the MVP pattern because I give the presenter direct access to the user control instead of by interface. Are either one of these methods fine or is there a better solution?

1

1 Answers

1
votes

These two methods

  public void OnAddTipButtonClicked(object sender, EventArgs e)
        {
            addTipControl.BringToFront();
        }

        public void OnHomeButtonClicked(object sender, EventArgs e)
        {
            homeControl.BringToFront();
        }

are not using the parameters that they have, also, if you will update something in the view that does not require any action/logic being made inside the Presenter layer, just do it inside your view.

For example, if you need to bring to front anything in your view, you can just do it there.

If you need a result that handles the presenter in order to bring to front that ui element, is a good practice to handle it inside your presenter.