多次更改按钮文本c#

时间:2014-10-24 23:25:26

标签: c#

我正在尝试这样做:

GUI仅使用一个文本框来接受三个整数。 创建 二 控件:文本框和按钮。 该 按键 小号 应该出现 文本框旁边 和 习惯了 输入在文本框中输入的整数值。 按钮 表示要输入的整数。例如,何时 输入第一个整​​数, 按键 (内容) 显示“输入第一个整​​数。”成功输入第一个整​​数后, 内容 改变为 “输入第二个整数。”输入第二个整数后, 内容 更改为“输入第三个整数”。 在第三个整数之后 进入 ,布 tton应该被禁用 并且应该显示按钮 “输入第一个整​​数” 试。

这是我到目前为止所管理的


private void button1_Click(object sender,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;

除了整数的部分,如果 - 其他是最接近我得到这个工作。我似乎不能说出来,以便if不会干扰其他部分。上面的例子在我第一次点击按钮后禁用它,如果我删除了它,启用它只是让我在"输入第一个整​​数"


我的问题: 我是以错误的方式来做这件事的吗?我应该使用if / else以外的东西吗?

我一直试图找到与此类似的内容,看看我做错了什么(循环通过多个文本框的按钮+通过点击事件做事)并且什么都没发现。这是一件奇怪的事情,很少发生,或者我只是在搜索错误的短语?

4 个答案:

答案 0 :(得分:2)

大概是当用户输入一个号码时,你就把它放在某处。你是如何决定把它放在哪里的?您最好有一个数组,当然您可以使用索引来确定新数字的位置。但即使您将数字放入单独命名的变量中,索引仍然最适合跟踪输入序列中的位置。

然后你需要这样的东西:

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语句的值,该语句确定哪个命名变量采用当前输入的值。

如果你想获得想象力,请将字符串[]放入资源。

答案 1 :(得分:1)

您应该使用if-else if-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;

    }

以上只会一次运行一个。你的原始代码的问题是它从第二个变为第三个,然后是下一个如果运行它返回到一个。

答案 2 :(得分:1)

比较字符串(以及代码中的复制粘贴字符串)不是一个好主意,而是使用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();
}

答案 3 :(得分:0)

我会将字符串存储为常量,但如果您不直接比较它们或在多个地方使用它们,则不需要这些字符串。我会使用整数而不是字符串来跟踪状态,因为字符串比较很容易失败(错别字,套管等)。最后,我会对int使用某种简单的验证,比如int.Parse(),以便在继续之前查看该条目是否有效。

示例:

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;
}