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?