Loop problem with array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alkis Arxontas
    New Member
    • Mar 2011
    • 10

    Loop problem with array

    I am using Visual Basic Express 2008.
    I'm really new to this, so this one could sound really kind of a joke to you, but the thing I want to do is:
    Have a text box (Textbox1) and I'd like by clicking a button to display the next array item starting from the first one, but actually it does only show the last one (comments(2)) everytime I click on the button. This is the code I added to my button:

    Private Sub Button6_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button6.Click
    Dim comments(2) As String
    comments(0) = "Some text1.."
    comments(1) = "Some text2.."
    comments(2) = "Some text3.."

    For i = 0 To 2
    TextBox1.Text = comments(i)
    Next

    End Sub
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    The For ...Next runs so fast trough the data that the textbox has no time to refresh, so you see only the last text.

    If you want to see the text with a time interval, you have to ad a timer.

    Comment

    • migules
      New Member
      • Mar 2012
      • 1

      #3
      Hi Alkis,

      Try this code

      TextBox1.Text = ""
      For i = 0 To 2
      TextBox1.Text = TextBox1.Text & vbcrlf & comments(i)
      Next

      Comment

      • rekedtechie
        New Member
        • Feb 2012
        • 51

        #4
        why you need to loop if you need to show only 1 result without any comparison?

        'declare a global varialble X
        'As Integer

        Dim X As Integer

        Public Sub Button6_Click()
        dim strStatements(2 ) as String
        strStatements(0 ) = "statement 1"
        strStatements(1 ) = "statement 2"
        strStatements(2 ) = "statement 3"
        if X > 2 Then
        exit(0);
        Else
        Textbox1.Text=s trStatements(X)
        X += 1
        End if
        End Sub

        'PS: your code is correct but..
        'you forgot to put your looping variable in the Next function

        'it should be like this "Next i"

        Comment

        Working...