A coworker of mine brought this scenario to me: If a Form has three text boxes on it, and the first text box is filled out, I want to enable the other two text boxes. If the first text box has all text removed (nothing filled out), I want to disable the other two text boxes. Here is the very quick, very contrived example of doing that, that I came up with. Please note that there is no real error handling or other "production level" code in here. It's purely an example and should be considered throw-away code, aside from being a quick example of how to implement that scenario.
public Interface IMyView
{
void EnableThoseThings();
void DisableThoseThings();
string Something{ get; }
string AnotherThing{ get; }
}
public class MyPresenter
{
private IMyView view = null;
public MyPresenter(IMyView myview)
{
view = myview;
}
public void SomethingChanged()
{
if (view.Something != string.Empty || view.AnotherThing != string.Emtpy)
view.EnableThoseThings();
else
view.DisableThoseThings();
}
}
public class MyForm: Form, IMyView
{
MyPresenter presenter = null;
protected void Form_Load(object sender, EventArgs e)
{
presenter = new MyPresenter(this);
}
protected void txtSomething_OnChanged(object sender, EventArgs e)
{
presenter.SomethingChanged();
}
public string Something
{
get { return txtSomething.Text; }
}
public string AnotherThing
{
get { return txtAnotherThing.Text; }
}
public void EnableThoseThings()
{
txtAnotherThing.Enabled = true;
txtWhatever.Enabled = true;
}
public void DisableThoseThings()
{
txtAnotherThing.Enabled = false;
txtWhatever.Enabled = false;
}
}
Hope this helps someone understand a simple scenario in MVP, a little more. :)