Skindeep2366
Skindeep2366

Reputation: 1559

textbox text not Updating while for each loop running

Windows Forms, VB application... Forgive me in advance for this very elementary question but I am overlooking something really simple here. I would like to have a label show the current value of a variable as the for each loop runs.. I created a very simple Button Click event to figure out where its failing. As it is right now the value does not display until after the for each loop finishes running, which it should actually display the counter value each time it runs through... I know this is possible and really simple because I did it years back but for the life of me cant figure out why I am missing the bar as it should constantly update the label to reflect that? Test Sub is as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim counter As Integer = 0
    For x As Integer = 1 To 200000
        counter += 1
        Label1.Text = Convert.ToString(counter)
    Next
End Sub

Upvotes: 0

Views: 10920

Answers (2)

John Woo
John Woo

Reputation: 263703

try this

For x As Integer = 1 To 200000
    counter += 1
    Label1.Text = Convert.ToString(counter)
    System.Threading.Sleep(200)
Next

I would advise you to use BackGroundWorker if you want to have a report progres.

SEE HERE
SEE ALSO HERE
BEST LINK for me and this

Upvotes: 1

Akhilesh B Chandran
Akhilesh B Chandran

Reputation: 6608

Try adding the line: Application.DoEvents() inside the loop.

Upvotes: 5

Related Questions