0

I've got a text box called txtbox, and have numbers in an array called number, I need to display the numbers in this array into the textbox in an event procedure (the user will click on Next and i have to show the next number in the number array), I'm fairly new to vb, what i have so far is.

Dim number() As Integer

 Dim i As Integer

  For i = 0 to number.Length -1

   Me.txtbox.Text = number(i)

Next 
2
  • your question says I need to display the numbers in this array in the textbox but in your code you ware assigning the textbox value to the arrat as number(i) = Me.txtbox.Text Commented Oct 16, 2014 at 7:18
  • @NeethuSoman i need to display the numbers in the array INTO the textbox, is that clear enough? maybe Me.txtbox.Text = number(i) is more clear enough? Commented Oct 16, 2014 at 7:21

3 Answers 3

3

Presuming that your question is not how to correctly initialize the array but how to access it to show the numbers in the TextBox. You can use String.Join, for example:

txtbox.Text = String.Join(",", number) ' will concatenate the numbers with comma as delimiter

If you only want to show one number you have to know which index of the array you want to access:

txtbox.Text = numbers(0).ToString()  ' first
txtbox.Text = numbers(numbers.Length - 1).ToString()  ' last

or via LINQ extension:

txtbox.Text = numbers.First().ToString()
txtbox.Text = numbers.Last().ToString()

If you want to navigate from the current to the next you have to store the current index in a field of your class, then you can increase/decrease that in the event-handler.

Sign up to request clarification or add additional context in comments.

Comments

1

To make it simple and use your code:

Me.txtbox.Clear()

For i = 0 to number.Length -1

   Me.txtbox.Text &= " " & number(i)

Next 

Me.txtbox.Text = Me.txtbox.Text.Trim

Comments

0

i suggest a scenario in this in each click of button you will get numbers from the array in sequential order; consider the following code

Dim clicCount As Integer = 0 ' <--- be the index of items in the array increment in each click
Dim a(4) As Integer '<---- Array declaration
a = {1, 2, 3, 4} '<---- array initialization
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If clicCount <= a.Length - 1 Then
        TextBox2.Text = a(clicCount)
        clicCount += 1
    End If
End Sub

1 Comment

that's workable, but a bit too complicating and a bit too much work for a noob such as myself, how about looping through all the values in the array and just displaying them in the textbox?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.