Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
So, HI!

How do i do the following thing:
I want the text in label1 to change every time I press i button. For example:

String text = "hello", "elephant", "pig", "dog";
<pre>private void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = text;
}






so the first time that i press the button it shows hello, next time elephant, next time pig and the last time dog and then hello again etc...

Thank you in advance! :laugh:
Posted

String[] text = { "hello", "elephant", "pig", "dog" };

int count=0;
private void Button1_Click(object sender, EventArgs e)
{
    Label1.Text=text[(count%text.Length)];
    count++;
}
 
Share this answer
 
Comments
euhiemf 12-Dec-10 7:17am    
Thank you! this works!
Try something like -

int count=0;
private void Button1_Click(object sender, EventArgs e)
{
    if (count > text.Length)
    {
           count = 0;
    }
    Label1.Text = text[count++];
}
 
Share this answer
 

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