多次更改按钮的文本c#
本文关键字:文本 按钮 | 更新日期: 2023-09-27 18:27:50
我正在尝试这样做:
GUI只使用一个文本框来接受这三个整数。创造二控件:一个文本框和一个按钮。这个按钮s应该出现文本框旁边和习惯于输入在文本框中输入的整数值。按钮指示要输入的整数。例如,当输入第一个整数按钮(内容)显示"Input first integer"。成功输入第一个整数后所容纳之物更改为输入第二个整数所容纳之物更改为"输入第三个整数"在第三个整数输入,the button应禁用并且按钮应该显示"输入第一个整数"再一次
这就是我迄今为止管理的
private void button1_Click(对象发送方,EventArgs e){
if (button1.Text == "Input First Integer")
{
button1.Text = "Input Second Integer";
}
else
{
button1.Text = "Input Third Integer";
}
if (button1.Text == "Input Third Integer");
{
button1.Text = "Input First Integer";
button1.Enabled = false;
抛开带整数的部分不谈,如果else是我最接近实现这一点的话。我似乎无法用短语表达它,这样if就不会干扰它的其他部分。上面的例子在我第一次点击按钮后禁用了它,如果我删除了启用的位,它只会让我处于"输入第一个整数"
我的问题是:我是不是搞错了?我应该用if/else以外的东西吗?
我一直在试图找到类似的东西,看看我做错了什么(在多个文本框条目中循环的按钮+通过点击事件做事情),但一无所获。这是一件很少发生的奇怪事情,还是我只是在谷歌上搜索了错误的短语?
假设当用户输入一个数字时,你就把它放在了某个地方。你是怎么决定把它放在哪里的?最好你有一个数组,当然你要使用一个索引来确定新数字的位置。但是,即使您将数字放入单独命名的变量中,索引仍然是跟踪您在输入序列中的位置的最佳方法。
那么你只需要这样的东西:
private static readonly string[] _buttonText =
{
"Input First Integer",
"Input Second Integer",
"Input Third Integer",
};
private int _inputState;
private void button1_Click(object sender, EventArgs e)
{
// Process current input here.
// Then update the button text for the next input number
// (wraps around to the first button text after the last
// button text was used)
_inputState = (_inputState + 1) % _buttonText.Length;
button1.Text = _buttonText[_inputState];
}
请注意,_inputState
索引也可用于为存储值的数组编制索引,或用作switch
语句的值,该语句确定哪个命名变量采用当前输入的值。
如果你想获得灵感,可以将字符串[]放入资源中。
您应该使用if-else-if-else(else仅在需要时使用)
if (button1.Text == "Input First Integer")
{
button1.Text = "Input Second Integer";
}
else if (button1.Text == "Input Second Integer")
{
button1.Text = "Input Third Integer";
}
else if (button1.Text == "Input Third Integer")
{
button1.Text = "Input First Integer";
button1.Enabled = false;
}
以上内容一次只运行一个。原始代码的问题是,它从第二位到第三位,然后是下一位。
比较字符串(并在代码中复制粘贴字符串)不是一个好主意,而是使用int
变量来存储步骤号:
private const int TotalSteps = 3;
private int step;
private void nextButton1Text()
{
switch (step % TotalSteps)
{
case 0:
button1.Text = "Input First Integer";
break;
case 1:
button1.Text = "Input Second Integer";
break;
case 2:
button1.Text = "Input Third Integer";
break;
}
button1.Enabled = step >= TotalSteps;
step++;
}
private void button1_Click(object sender, EventArgs e)
{
nextButton1Text();
}
我会将字符串存储为常量,尽管如果你不打算直接比较它们或在多个地方使用它们,这是不必要的。我会使用整数而不是字符串来跟踪状态,因为字符串比较很容易失败(打字错误、大小写等)。最后,我会对int使用一些简单的验证,比如int.Passe(),在继续之前查看条目是否有效。
示例:
private const string InputFirst = "Input first integer";
private const string InputSecond = "Input second integer";
private const string InputThird = "Input third integer";
private int integersEntered;
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = InputFirst;
}
private void button1_Click(object sender, EventArgs e)
{
if (ValidateInput(textBox1.Text))
{
textBox1.Clear();
switch (integersEntered)
{
case 1:
button1.Text = InputSecond;
break;
case 2:
button1.Text = InputThird;
break;
case 3:
button1.Text = InputFirst;
button1.Enabled = false;
break;
}
}
else
{
MessageBox.Show("You must enter a valid integer.", "Text Validation");
textBox1.Focus();
textBox1.SelectAll();
}
}
private bool ValidateInput(string input)
{
int value;
var success = int.TryParse(input, out value);
if(success) { integersEntered++; }
return success;
}