By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Cant get label to update label.text

P: n/a
Hello, Am new to C# and am playing around trying to get a while loop to
display a int on a windows console using label.

private void btnGo_Click(object sender, System.EventArgs e)
{
int Counter = 0;
while ( Counter < 10 )
{
lblMain.Text =Counter.ToString();
System.Threading.Thread.Sleep(5000);
Counter++;
} //WHile

This is the button the starts the loop which is supposed to show the
value of Counter on the label (lblMain) text field....if I run the loop
it runs fine but doesnt change the value of the text field until right
at the end...when it displays a 9. I have included the Sleep command
incase it was running too fast for me to see, but that isnt the
case....

Any clever comments anyone?

Thanks

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Sorry - probably didnt make the question clear - why cant I see the
variable Counter counting up from 1 to 9?

Nov 17 '05 #2

P: n/a
Hello kaiser,

Just add one line:
int Counter = 0;
while ( Counter < 10 )
{
label1.Text =Counter.ToString();
Application.DoEvents(); //Refresh(); and Update(); also work. You have
to choose the right one for you.
System.Threading.Thread.Sleep(2000);
Counter++;
}

-----
Ariel Popovsky
Hello, Am new to C# and am playing around trying to get a while loop
to display a int on a windows console using label.

private void btnGo_Click(object sender, System.EventArgs e)
{
int Counter = 0;
while ( Counter < 10 )
{
lblMain.Text =Counter.ToString();
System.Threading.Thread.Sleep(5000);
Counter++;
} //WHile
This is the button the starts the loop which is supposed to show the
value of Counter on the label (lblMain) text field....if I run the
loop it runs fine but doesnt change the value of the text field until
right at the end...when it displays a 9. I have included the Sleep
command incase it was running too fast for me to see, but that isnt
the case....

Any clever comments anyone?

Thanks

Nov 17 '05 #3

P: n/a
Great...thanks

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.