简体   繁体   中英

Label Increment on Button Click with For Loop C#

I am trying to get a label to increase by 1 with each button click up to 5 then revert back to 1 and start again. However, I seem to be entering my for loop incorrectly. Could anyone point out where I'm going wrong? Very new to 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();
    }
}

-The label text is defaulted to 1.

Thank you

When the button is clicked, you change the value of the label, incrementing its current value.
This solution uses the % 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();
    }
}



Explaining the % operator
"The % operator computes the remainder after dividing its first operand by its second"
So in this case the results will be:

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

And when the current value is 5 this is going to happen:

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

If I understand what you want:

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

Most likely you will need the value of the label for your business logic - to place a bet. I think you should have a private variable for it, increment it from the button onclick event, then copy it into the label textbox.

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

Try this:

    int bet = 1;

    private void button1_Click(object sender, EventArgs e)
    {
        bet++;

        if (bet == 6)
            bet = 1;                

        lblBet.Text = bet.ToString();
    }

Bet variable needs to be declared outside of the function.

You can try this:

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

No need for a for loop. Initialize bet outside of button click:

int bet = 1;
private void bttnAdd_Click(object sender, EventArgs e)
{


   if (bet <= 6)
   {
       this.bet++;
       lblBet.Text = bet.toString();
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM