Search code examples
c#winformsmvp

Loading subviews with MVP


I have been playing around with the MVP pattern using winforms for the last couple of days, there is only really one thing that I'm not sure how to do. How do you create a subform from another view. Would this be a valid option.

 public class MyForm : IMainFormView
    {
        private MainFormPresenter pres;
        public MyForm() { pres = new MainFormPresenter(this); }

        //Event from interface.
        public event EventHandler<EventArgs> LoadSecondForm;

        public void SomeButtonClick()
        {
            LoadSecondForm(this, EventArgs.Empty);
        }
    }

    public class MainFormPresenter
    {
        private IMainFormView mView;

        public MainFormPresenter(IMainFormView view) {
            this.mView = view;
            this.Initialize();
        }

        private void Initialize() {
            this.mView.LoadSecondForm += new EventHandler<EventArgs>(mView_LoadSecondForm);
        }


        private void mView_LoadSecondForm(object sender, EventArgs e) {
            SecondForm newform = new SecondForm(); //Second form has its own Presenter.
            newform.Load(); // Load the form and raise the events on its presenter.
        }
    }

I'm mainly concerned with how you would load a subform using this pattern, and how you would pass say an ID from the first page to the subform.

Thanks.


Solution

  • http://www.rickardnilsson.net/post/The-Humble-dialog-v2.aspx

    The link above is the closest I've seen to answering this. Most attempts at solving this leave something to be desired.