【问题标题】:Change text of button multiple times c#多次更改按钮的文本c#
【发布时间】:2014-10-24 23:25:25
【问题描述】:

我正在尝试这样做:

GUI 只使用一个文本框来接受三个整数。 创造 二 控件:一个文本框和一个按钮。 这 按钮 s 应该出现 文本框旁边 和 习惯于 输入在文本框中输入的整数值。 按钮 表示要输入的整数。例如,当 输入第一个整​​数,则 按钮 (内容) 显示“输入第一个整​​数”。成功输入第一个整​​数后, 内容 更改为 “输入第二个整数。”输入第二个整数后, 内容 更改为“输入第三个整数”。 在第三个整数之后 进入 , 卜 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-else 是我最接近让它工作的部分。我似乎无法表达它,以便 if 不会干扰它的其他部分。上面的示例在我第一次单击按钮后禁用它,如果我通过启用它删除该位,它只会让我留在“输入第一个整​​数”


我的问题: 我会以错误的方式解决这个问题吗?我应该为此使用 if/else 以外的东西吗?

我一直在尝试找到与此类似的内容,以查看我做错了什么(在多个文本框条目之间循环的按钮 + 通过单击事件执行操作),但什么也没找到。这是很少出现的奇怪事情还是我只是在谷歌上搜索了错误的短语?

【问题讨论】:

    标签: c#


    【解决方案1】:

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

    那么你只需要这样的东西:

    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 语句将其设置回第一个按钮。
    • @deathismyfriend:谢谢。我更喜欢算术,但你是对的......代码需要防止以某种方式从string[] 的末尾脱落。 :)
    • 您应该使用 If 语句或 ++_inputState = (_inputState = buttonText.Length) { _inputState = 1; }
    • @deathismyfriend:抱歉,我之前的评论并不清楚。我已经编辑了答案以解决您的问题(即使用算术解决方案而不是条件/三元方法)。
    【解决方案2】:

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

    以上内容一次只运行一个。您的原始代码的问题是它从第二个到第三个,然后如果运行下一个,它又返回到一个。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        我会将字符串存储为常量,但如果您不打算直接比较它们或在多个地方使用它们,这不是必需的。我会使用整数而不是字符串来跟踪状态,因为字符串比较容易失败(错别字、大小写等)。最后,我将对 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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-11
          • 1970-01-01
          • 2017-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多