TextBox.text value to display a value from For Next loop

studio72

Member
Joined
Sep 26, 2010
Messages
6
Programming Experience
Beginner
Hi,
I have a problem, and of course I am a beginer in VB coding :(
I want to display a value from For Next Loop in one textbox, something like that :


For x = 1 To 1500
TextBox1.Text = x.ToString
System.Threading.Thread.CurrentThread.Sleep(2000)
Next

I want to display in textbox the value for x in loop

Thanks
 
Hi,

What you have coded does actually work but it's that way you have coded it that makes it look like it does not work.

You must remember that a simple for loop, in this case 1 to 1500, will perform amazingly fast. Without doing an actual timing it will complete within 100 milliseconds give or take a few milliseconds. Due to this, you effectively are putting your system to sleep for 1500 x 2 seconds, being 50 minutes and your TextBox does not have time to refresh itself.

To get round this in the code you have provided you need to force the TextBox to refresh every time you add a number to it. See here:-

VB.NET:
For x = 1 To 1500
  TextBox1.Text = x.ToString
  TextBox1.Refresh()
  Threading.Thread.Sleep(2000)
Next

Other than just seeing how a for loop works is there a particular reason why you coded this the way you did using Threading.Thread.Sleep(2000)? There are better ways to do things like this using Timers.

Hope that helps.

Kind regards,

Ian
 
A timer and a counter would be more appropriate.
If you actually have processing taking time that should be done in a secondary thread, and not in UI thread. For that a BackgroundWorker may be useful.
 
Thanks folks
I found the way for my isue
The resolution is "Application.DoEvents()"
I manipulate many files in For Next Loop, and I need to know where the loop is.
Now is ok
For x = 1 To 1500
TextBox1.Text = x.ToString
Application.DoEvents()
Next
 
Back
Top