Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi .
i have a button which displays the value of "i" in Messagebox. i want to display the value as 1 for 1sttime, and exit the loop,
then for button click again it should display as 2
and exit the loop
then for button click again it should display as 3
and exit the loop
then for button click again it should display as 4
and exit the loop
C#
private void Button_Click(object sender, RoutedEventArgs e)
       {
           c = 1;
           for (i = 1; i <= 26; i++)
           {
               //  b = i;
               c = i;
               MessageBox.Show(" value of i is " + i);
           }

how should i change this code to get that..
Posted
Updated 6-May-15 21:07pm
v2

1 solution

You don't want a loop, just move the variable outside the method:
C#
private int counter = 1;
private void Button_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("The value of counter is " + counter++);
}
 
Share this answer
 
Comments
CPallini 7-May-15 4:05am    
5.
Aditya_Goud 7-May-15 6:01am    
what 5 ??
OriginalGriff 7-May-15 6:20am    
It's part of the voting system here: next to questions and answers you will see a number of stars (they may be solid orange, or "hollow" orange. Solid ones are "good" votes, empty ones are "bad votes" or "no votes yet".
If you drag your mouse over them, you can change the vote you are going to give - 5 stars is "good question/answer - that helped", 1 star is "bad question/answer - that was useless". clicking "sets" the vote.

1 star is a downvote, and affects reputation by taking points away.
5 stars is an upvote, and adds points to reputation.
You can also use "Accept" to say "this resolved my problem" and that also adds points to reputation.
Your reputation is currently pretty low (because you haven't been here long) and you can see how it is made up by looking at your profile page - just click on your username.

So Carlo was saying "5." to indicate he agrees with me, and has upvoted the answer.

Make sense?

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