AvocadoSoftware.com

Software For Hardcore Developers
Welcome to AvocadoSoftware.com Sign in | Join | Help
in Search

Derick Baileys old blog archives - go to derickbailey.com for new contents

Model View Presenter: Example of Enabling / Disabling TextBoxes

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. :)

Published Wednesday, April 18, 2007 1:48 PM by dredge
Filed Under: , , ,
New Comments to this post are disabled

This Blog

Post Calendar

<April 2007>
SuMoTuWeThFrSa
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Advertisement

News

this is my old blog archives - go to http://derickbailey.com for updates

Syndication

Advertisement

Powered by Community Server, by Telligent Systems