dax
dax

Reputation: 203

C# Variable labels?

Say I have a box that says ENABLED or DISABLED. How can I make the text vary depending on a state?

Upvotes: 0

Views: 870

Answers (5)

MedicineMan
MedicineMan

Reputation: 15314

The last few months I have been going with a slightly lighter weight solution than implementing a whole class to manage state. I usually define an enum which indicates the types of states available in the UI, then I have a function that makes changes to the UI, based on the state selected. This approach has been very successful, and not too heavy in terms of the amount of code needed to be written.

If I want to know what states are available in the UI, I can check the values of the enum.

public enum SystemState
{
    /// <summary>
    /// System is under the control of a remote logging application.  
    /// </summary>
    RemoteMode,

    /// <summary>
    /// System is not under the control of a remote logging application.
    /// </summary>
    LocalMode
}

public interface IView
{
    void SetState(SystemState state);
}


//method on the UI to modify UI
    private void SetState(SystemState state)
    {
        switch (state)
        {
            case SystemState.LocalMode:

                //for now, just unlock the ui
                break;
            case SystemState.RemoteMode:

                //for now, just lock the ui
                break;
            default:
                throw new Exception("Unknown State requested:" + state);
        }
    }


    //now when you change state, you can take advantage of intellisense and compile time checking:
    public void Connect()
    {
            SetState(SystemState.RemoteMode);
    }

Upvotes: 0

Rex M
Rex M

Reputation: 144112

If I understand correctly, you are asking how to have a label or some other bit of UI text automatically update to reflect a "state variable". This is just one way to accomplish what you're describing:

I would do it by having a central state object which implements INotifyPropertyChanging and INotifyPropertyChanged. When your application initializes, attach event handlers to the events those interfaces expose, and one of those event handlers can change the text of the label when property (Foo) changes.

public class State : INotifyPropertyChanging, INotifyPropertyChanged
{
    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanging(PropertyChangingEventArgs e)
    {
        if (this.PropertyChanging != null)
        {
            this.PropertyChanging(this, e);
        }
    }

    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, e);
        }
    }

    public bool Foo
    {
        get
        {
            return foo;
        }
        set
        {
            if (value != foo)
            {
                this.OnPropertyChanging(new PropertyChangingEventArgs("Foo"));
                foo = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("Foo"));
            }
        }
    }

    private bool foo = false;
}

protected void HandleStateChanged(object sender, PropertyChangedEventArgs e)
{
    if(e.PropertyName == "Foo")
    {
        box.Text = state.Foo ? "Enabled" : "Disabled";
    }
}

Upvotes: 2

harley.holt
harley.holt

Reputation: 3

What jeffamaphone said, but I should add that any time that state changes you will have to make sure to run that same code. The easiest way to insure this happens is by binding the box.Text property to the state object that you are interested in. That way, any change made to the object is immediately reflected in the text.

This blog post helped me get started with data binding.... because I love FAQs.

Upvotes: 0

hmcclungiii
hmcclungiii

Reputation: 1805

public void CheckBox1CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked) {
        checkBox1.Text = "Enabled";
    }
    else {
        checkBox1.Text = "Disabled";
    }
}

Upvotes: 4

i_am_jorf
i_am_jorf

Reputation: 54600

box.Text = (box.Enabled ? "ENABLED" : "DISABLED");

Upvotes: 2

Related Questions