如何将数组中的整数显示到文本框中

时间:2014-10-16 07:12:08

标签: arrays vb.net

我有一个名为txtbox的文本框,并且在一个名为number的数组中有数字,我需要在事件过程中将此数组中的数字显示到文本框中(用户将单击Next并且我有为了显示数字数组中的下一个数字,我对vb相当新,到目前为止我是。

Dim number() As Integer

 Dim i As Integer

  For i = 0 to number.Length -1

   Me.txtbox.Text = number(i)

Next 

3 个答案:

答案 0 :(得分:3)

假设您的问题不是如何正确初始化数组,而是如何访问它以显示TextBox中的数字。您可以使用String.Join,例如:

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

如果您只想显示一个数字,您必须知道要访问的数组的索引:

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

或通过LINQ扩展名:

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

如果要从当前导航到下一个导航,则必须将当前索引存储在类的字段中,然后可以在事件处理程序中增加/减少该索引。

答案 1 :(得分:1)

为了简单起见并使用您的代码:

Me.txtbox.Clear()

For i = 0 to number.Length -1

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

Next 

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

答案 2 :(得分:0)

我建议在每次单击按钮的情况下,您将按顺序从数组中获取数字;请考虑以下代码

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
相关问题