C# 单击后更改按钮文本,然后再次单击后将其更改回来
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15400421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change button text after click, then change it back after clicking again
提问by Arnulfo Arroyo
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.
按钮从“ON”开始。当我第一次点击它时,它应该变成“OFF”,当我再次点击它时,它应该变回“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";
}
采纳答案by Pyromancer
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.
希望这可以帮助。
回答by MattW
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):
编辑:正如 Cody 在下面的评论中指出的那样,这不适用于本地化。对于更容易本地化的内容,请使用 ViewState(假设初始文本是打开订单):
bool ordersWereOn = (ViewState["OrdersAreOn"] as bool?) ?? false;
ViewState["OrdersAreOn"] = !ordersWereOn;
OrdersButton.Text = ordersWereOn ? "Turn Orders On" : "Turn Orders Off";
回答by Roman Gruber
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...
编辑:另外,使用复选框或切换按钮不是更有意义吗?除了视觉表现不同之外,它还可以立即执行您想要的操作……
回答by Yeldar Kurmangaliyev
There are a lot of ways to do this.
As for me, I would probably use Tag
attribute of Control
because:
有很多方法可以做到这一点。
至于我,我可能会使用Tag
属性,Control
因为:
- It is being stored into a button's variable. You don't need any external variables
- All code can be written in a single method
- 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:
您还可以设置默认Tag
值(真或假)并删除对空值的检查。您也始终可以通过以下方式访问它:
public void DoSomeImportantThings()
{
var areOrdersTurnedOn = (bool)OrdersButton.Tag;
}
It is not the best approach - it is just another one.
For example, Roman Gruberhas shown, probably, the best answer using properties with text changing logic encapsulated into a setter.
这不是最好的方法——它只是另一种方法。
例如,Roman Gruber可能已经展示了使用封装到 setter 中的文本更改逻辑的属性的最佳答案。
回答by Kamal Shetty
private void button4_Click(object sender, EventArgs e)
{
if (button4.Text == "Show Password")
{
textBox2.PasswordChar = '##代码##';
button4.Text = "Hide Password";
}
else
{
textBox2.PasswordChar = '*';
button4.Text = "Show Password";
}
}