Change button text after click, then change it bac

2020-02-26 07:45发布

问题:

I am trying to change the text of a Button every time its clicked.

Button Starts as "ON". When I click it the first time it should change to "OFF", and when I click it again, it should change back to On.

I understand how to change it to "OFF" when clicked, but I am unsure as to how to check for a secondary click so I can change the text back to "ON".

Here is my code so far:

private void OrdersButton_Click(object sender, EventArgs e)
{
       OrdersButton.Text = " Turn Orders Off";
}

回答1:

Try

    private void OrdersButton_Click(object sender, EventArgs e)
    {
        if (OrdersButton.Text == "Turn Orders On")
        {
            OrdersButton.Text = "Turn Orders Off";
        }
        else if (OrdersButton.Text == "Turn Orders Off")
        {
            OrdersButton.Text = "Turn Orders On";
        }
    }

Hope this helps.



回答2:

I can see several possible problems with that and can offer a more object oriented solution: Adding a property that keeps track of the current "state":

    private bool _IsOn;

    public bool IsOn
    {
        get
        {
            return _IsOn;
        }
        set
        {
            _IsOn = value;
            OrdersButton.Text = _IsOn ? "On" : "Off";
        }
    }

and using the event handler to simply toggle the property:

private void OrdersButton_Click(object sender, EventArgs e)
{
     IsOn = !IsOn;
}

That way it's easier to access the information later on and you can easily replace on/off with whatever you like - even globalize/localize it if required. I think it's a very bad programming practice to have code depend on the text that is on the display...

EDIT: Also, wouldn't using a checkbox or a togglebutton make more sense? Other than the visual representation being different, it does what you want out of the box...



回答3:

Just test the current text:

OrdersButton.Text = OrdersButton.Text.EndsWith("Off") ? "Turn Orders On" : "Turn Orders Off";

Edit: As Cody points out in comments below, that doesn't work well with localization. For something that can more easily be localized use ViewState (assuming the initial text is to turn orders on):

bool ordersWereOn = (ViewState["OrdersAreOn"] as bool?) ?? false;
ViewState["OrdersAreOn"] = !ordersWereOn;
OrdersButton.Text = ordersWereOn ? "Turn Orders On" : "Turn Orders Off";


回答4:

There are a lot of ways to do this.
As for me, I would probably use Tag attribute of Control because:

  1. It is being stored into a button's variable. You don't need any external variables
  2. All code can be written in a single method
  3. You can always access it from other methods \ controls

Here is the example code:

private void OrdersButton_Click(object sender, EventArgs e)
{
    bool value = (OrdersButton.Tag as bool?) ?? true;
    OrdersButton.Tag = !value;

    OrdersButton.Text = "Turn Orders " + (value ? "On" : "Off");
}

You might also set default Tag value (true or false) and remove checking for null value. You will also be always able to do access it this way:

public void DoSomeImportantThings()
{
    var areOrdersTurnedOn = (bool)OrdersButton.Tag;
}

It is not the best approach - it is just another one.
For example, Roman Gruber has shown, probably, the best answer using properties with text changing logic encapsulated into a setter.



回答5:

private void button4_Click(object sender, EventArgs e)
{
    if (button4.Text == "Show Password")
    {
        textBox2.PasswordChar = '\0';
        button4.Text = "Hide Password";
    }
    else
    {
        textBox2.PasswordChar = '*';
        button4.Text = "Show Password";
    }
}


标签: c# .net oop