【问题标题】:Label Increment on Button Click with For Loop C#使用 For 循环 C# 按钮单击时的标签增量
【发布时间】:2016-08-14 06:35:44
【问题描述】:

我试图让标签增加 1,每个按钮单击最多 5 次,然后恢复为 1 并重新开始。但是,我似乎错误地输入了我的 for 循环。谁能指出我哪里出错了?对 C# 非常陌生。

private void bttnAdd_Click(object sender, EventArgs e)
{
    int bet = 1;
    if (bet < 6)
    {
        for (int bet = 1; bet <= 6; bet++)
        {
            lblBet.Text = "" + bet;
        }
    }
    else
    {
        lblBet.ResetText();
    }
}

-标签文本默认为1。

谢谢

【问题讨论】:

  • 无法看到您的标签更改其文本。标签的更新只有在您退出事件处理程序后才会发生。
  • for循环的目的是什么?迭代太快了,肉眼看不到。
  • 没有循环点,只使用一个以1开头的grobal变量,每次用户点击按钮,只需将变量添加1,如果值为6,则重新设置,

标签: c# winforms for-loop label increment


【解决方案1】:

单击按钮时,您更改标签的值,增加其当前值。
此解决方案使用% Operator (C# Reference)

private void bttnAdd_Click(object sender, EventArgs e)
{
    int currentValue;
    // Tries to parse the text to an integer and puts the result in the currentValue variable
    if (int.TryParse(lblBet.Text, out currentValue))
    {
        // This equation assures that the value can't be greater that 5 and smaller than 1
        currentValue = (currentValue % 5) + 1;
        // Sets the new value to the label 
        lblBet.Text = currentValue.ToString();
    }
}



解释 % 运算符
“% 运算符计算第一个操作数除以第二个操作数后的余数”
所以在这种情况下,结果将是:

int currentValue = 1;
int remainderCurrentValue = currentValue % 5; // Equals 1
int increasedCurrentValue = remainderCurrentValue + 1; // Equals 2

当当前值为 5 时,会发生这种情况:

int currentValue = 5;
int remainderCurrentValue = currentValue % 5; // Equals 0
int increasedCurrentValue = remainderCurrentValue + 1; // Equals 1

【讨论】:

  • 非常感谢。我删除了 for 循环。我忽略了它不会在每次增量之间暂停。这很好用,减去它重置回 0 而不是 1。
  • @sploosh4snootch 是的,我的错!我写了错误的算法......但后来我编辑了我的答案,现在应该完成这项工作
  • 我很抱歉。我在刷新以查看您的编辑之前回复了。再次感谢!完美运行。
【解决方案2】:

如果我明白你想要什么:

int bet = 1;
bool increase=true;
private void bttnAdd_Click(object sender, EventArgs e)
{
   if(increase){
      bet++;
      lblBet.Text = "" + bet;
   }
   else{
       bet--;
       lblBet.Text = "" + bet;
   }
   if(bet==5 || bet==1)
   {
       increase=!increase;
   }
}

【讨论】:

    【解决方案3】:

    您很可能需要您的业务逻辑的标签值 - 下注。我认为你应该有一个私有变量,从按钮 onclick 事件中增加它,然后将它复制到标签文本框中。

                private void bttnAdd_Click(object sender, EventArgs e)
                {
                    int bet = int.Parse(lblBet.Text);
                    lblBet.Text = bet<5 ? ++bet : 1;
                }
    

    【讨论】:

      【解决方案4】:

      试试这个:

          int bet = 1;
      
          private void button1_Click(object sender, EventArgs e)
          {
              bet++;
      
              if (bet == 6)
                  bet = 1;                
      
              lblBet.Text = bet.ToString();
          }
      

      Bet 变量需要在函数外声明。

      【讨论】:

        【解决方案5】:

        不需要 for 循环。在按钮点击之外初始化下注:

        int bet = 1;
        private void bttnAdd_Click(object sender, EventArgs e)
        {
        
        
           if (bet <= 6)
           {
               this.bet++;
               lblBet.Text = bet.toString();
           }
        
        }
        

        【讨论】:

          【解决方案6】:

          你可以试试这个:

          static int count=0;// Global Variable declare somewhere at the top 
          
          protected void bttnAdd_Click(object sender, EventArgs e)
                  {
                      count++;
          
                      if (count > 6)
                      {
                          lblBet.Text = count.ToString();
                      }
                      else
                      {
                          count = 0;
                      }
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-11-29
            • 2020-07-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多