#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0

    Using loop in text box


    Hello I used textbox in my project i write a number and some thing writing i textbox using loop
    for example
    i used this code
    Private Sub Command1_Click()
    Dim i As Integer
    For i = 0 To 10
    Text1.Text = "work is great" & i
    Next i
    End Sub

    but when i run program and click button only see result work is great10

    i run
    work is great0
    work is great1
    work is great2
    work is great3
    work is great4
    .
    .
    .
    how can this work in textbox
  2. #2
  3. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,440
    Rep Power
    4539
    Try putting a DoEvents in your loop to refresh the textbox display on each iteration.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    23
    Rep Power
    0
    please send complete code thanks
    i know first DoEvents command used but i not know how used it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    45
    Originally Posted by kako0000000
    Hello I used textbox in my project i write a number and some thing writing i textbox using loop
    for example
    i used this code
    Private Sub Command1_Click()
    Dim i As Integer
    For i = 0 To 10
    Text1.Text = "work is great" & i
    Next i
    End Sub

    but when i run program and click button only see result work is great10

    i run
    work is great0
    work is great1
    work is great2
    work is great3
    work is great4
    .
    .
    .
    how can this work in textbox
    It can't. The Command is Event Driven, so anything that generates more events will end up in an infinite loop.

    J.A. Coutts
  8. #5
  9. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,440
    Rep Power
    4539
    Perhaps DoEvents is different in newer versions of VB, in VB6 DoEvents was needed to allow the UI to refresh within a subroutine. DoEvents turned the event queue over to windows, without it and depending on your code structure you wouldn't see visible changes until the code block completed, and if it was a lengthy (time) procedure your UI would appear hung.

    I haven't used VB since VB6 so I'm pretty much out of date here.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Most likely the code is executing so fast that you don't see any of the other lines as they are being generated. If you actually want to see each line you need to delay the execution of each loop iteration using the Win32 "Sleep" API like so.

    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Private Sub Command1_Click()
    Dim i As Integer
    For i = 0 To 10
      Text1.Text = "work is great" & i
      DoEvents
      Sleep 500
    Next i
    End Sub
    Note : If you actually want to see the whole list at once, you need to set the textbox property "Multiline" to "True", make it big enough to display all items and use the following loop:

    Code:
    Dim i As Integer
    For i = 0 To 10
      Text1.Text = Text1.Text & "work is great" & i & vbCrLf
      DoEvents
      Sleep 500
    Next i
    You could optionally dispense with the Sleep and DoEvents lines if you only need to see the whole list once it's all generated.

    cheers
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    99
    Rep Power
    157
    Originally Posted by couttsj
    It can't. The Command is Event Driven, so anything that generates more events will end up in an infinite loop.

    J.A. Coutts
    the problem is simply that you continually refresh the contents of the textbox and so there will only be blah blah 10 in the box at the end

    to see a list containing the output lines you need to make the textbox multilines or use a more appropriate structure like a listbox or rtftextbox

    here to help

IMN logo majestic logo threadwatch logo seochat tools logo