Click here to Skip to main content
16,011,358 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello There!

I have two buttons in C# and one should add to counter and another should substract from counter.

Label = 130

Click on "+" 130 become to 131 , click again 132 ...

What I have tried:

int counter = 0;
counter += counter + 1;


string numberString = "130";
int number = int.Parse(numberString);
Bpm_Counter.Content = number;
Posted
Comments
0x01AA 17-Sep-24 10:18am    
Make your life as a programmer easier. Don't take the text from the edit, but create an integer variable. You can count this up and down with e.g. counter++ and counter-- and every time this variable is changed you do ThisOrThatGuiElement.Content= counter.ToString()

So little code and so many problems.

First, you do not create your integer value in a string and then parse it. All you need to do is keep the value in an integer, like this:
C#
int value = 130;

Next, your counter increment is very, very wrong. It's not adding 1 to the counter. It's adding the counter to itself and then adding 1. So, if counter is 130 and you execute your statement, count will end up being 261, not 131. You increment should be one of the following:
C#
counter += 1;                 // or
counter = counter + 1;        // or
counter++;

All of these will do the same thing for your purposes.

And now you have a new variable, called number. What is this supposed to be? Are you trying to show the value of counter instead? Well, you have to tell it to use counter, not number.
C#
Bpm_Counter.Content = counter
 
Share this answer
 
Comments
Mark Rene Jensen 17-Sep-24 11:29am    
int counter = 130; counter = counter + 1; Bpm_Counter.Content = counter; stays in 131 only! Hmm should int counter stay in button action? It do not add a to counter somehow?
Dave Kreskowiak 17-Sep-24 11:57am    
I don't know what you're talking about and really can't answer further because I don't know anything about the rest of your code. I only get to work with what you type here.
int counter = 130;
Should be in "Global Value"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900