shaka
shaka

Reputation: 11

How to display integers from array into textbox

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 

Upvotes: 0

Views: 8521

Answers (3)

Shockwaver
Shockwaver

Reputation: 401

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

Upvotes: 1

user3972104
user3972104

Reputation:

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

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460058

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.

Upvotes: 3

Related Questions