Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, Guys, I want to know how to increase one number in the label on click I have a label in my win form I want when I click on this label so 1 number increase in this label for example if label.text = o; so when I click it so it make 1, then 2, then 3 soon is it possible please help me about it
Thanks

What I have tried:

for (int i = 0; i =< label1.Text; i++)
            { 
                
            }
Posted
Updated 14-Mar-17 3:06am
Comments
[no name] 14-Mar-17 8:49am    
Okaaaayyyy, what does a for loop have to do with your click event? Where is the code where you are converting the label text to a number and doing your addition?
Member 9983063 14-Mar-17 9:01am    
i am using this for loop on my click event
for (int i = 0; i =< label1.Text; i++)
{

}
CHill60 14-Mar-17 9:07am    
The point @NotPoliticallyCorrect is making is that you do not need a loop, you are not converting the label text to a number, you are not adding anything to the value.
[no name] 14-Mar-17 9:28am    
Why would you think that would do anything at all?
Karthik_Mahalingam 14-Mar-17 9:09am    
why do you need 'for loop" to display the count?

As an alternative to Solution 1, here is a solution that looks at the current contents of the label and increments whatever is already there (This code would be in your button's Click event)
var curr = 0;
if(int.TryParse(label1.Text, out curr))
    curr++;
label1.Text = curr.ToString();
 
Share this answer
 
Comments
Bryian Tan 14-Mar-17 9:08am    
Nice!!! I was thinking to post the same, but I was too slow :)
CHill60 14-Mar-17 9:13am    
:-) Fingers of fire
Graeme_Grant 14-Mar-17 9:19am    
I thought the same however the value has to be loaded at some point. Might as well show how to do it properly.
CHill60 14-Mar-17 9:28am    
"Properly" is subjective. If you note, my solution caters for the occasion where the label does not already contain a number. It is also completely self-contained and does not rely on a backing variable (used to be called global variables back in the day). General consensus seems to favour variable declaration as close as possible to usage (aka limiting scope of variable) - which is what several Code Review tools also suggest.
Graeme_Grant 14-Mar-17 9:31am    
I don't disagree. I live in the WPF MVVM world, not WinForm. So you need a variable to alter it.
Have an integer variable declared in the root of the code-behind...
C#
int Count = 0;
then when you want to update the label:
C#
label.Text = ++Count.ToString();
 
Share this answer
 
Comments
Bryian Tan 14-Mar-17 9:05am    
should be
label.Text = (++Count).ToString()
to avoid the error "The operand of an increment or decrement operator must be a variable, property or indexer"
Graeme_Grant 14-Mar-17 9:18am    
Yeah ... that too! lol

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



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