List Box Population

P

Paul Anderson

I have a newbie question that I hope someone can answer.
VB Studio 2005

I have a form with a button and a listbox. When the button is pressed
the following routine is run:

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim wrap As String
wrap = Chr(13) & Chr(10)
For i = 1 To 10
TextBox1.Text = TextBox1.Text & "Loop " & i & wrap
System.Threading.Thread.Sleep(1000)
Next i
End Sub
End Class

All this does is populate the listbox with loop1,loop2, etc with a crlf
at the end of each loopx.

While this is running I can see that the listbox is getting bigger
because the elevator bar is created and successively gets smaller as
more loops are run. However the words ("loopx") do not appear in the
listbox until the entire 10 iterations of the loop are complete.

Can anyone tell me why this happens? Is there a cure? Can I make it so
that each "loopx" line appears successively once per second in the box?

Thank you.

Paul Anderson
 
H

Henry

Paul Anderson said:
I have a newbie question that I hope someone can answer.
VB Studio 2005

I have a form with a button and a listbox. When the button is pressed
the following routine is run:

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim wrap As String
wrap = Chr(13) & Chr(10)
For i = 1 To 10
TextBox1.Text = TextBox1.Text & "Loop " & i & wrap
System.Threading.Thread.Sleep(1000)
Next i
End Sub
End Class

Listbox, or textbox?

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button3.Click

Dim i As Integer

For i = 0 To 10

ListBox1.Items.Add("text " + CStr(i))

Next i

End Sub



cheers

Henry
 
P

Paul Anderson

Henry said:
Listbox, or textbox?

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button3.Click

Dim i As Integer

For i = 0 To 10

ListBox1.Items.Add("text " + CStr(i))

Next i

End Sub



cheers

Henry
Henry,
Thanks for the response but it's a textbox so items.add won't work.
The output is produced, but only after the procedure has completed. I'd
like it to refresh while looping through the procedure.
Paul
 
G

Guest

Your code should be:
TextBox1.Text = TextBox1.Text & "Loop " & i & wrap
Me.Refresh()
System.Threading.Thread.Sleep(1000)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top