Jret
Jret

Reputation: 90

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

Upvotes: 1

Views: 2906

Answers (6)

Geeky Ninja
Geeky Ninja

Reputation: 6052

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

Upvotes: 0

Tommaso Bertoni
Tommaso Bertoni

Reputation: 2381

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

Upvotes: 3

born2bmild
born2bmild

Reputation: 144

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

}

Upvotes: 0

peter kover
peter kover

Reputation: 77

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.

Upvotes: 0

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

Upvotes: 0

Nejc Galof
Nejc Galof

Reputation: 2606

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

Upvotes: 0

Related Questions