vb.net 如何将数组中的整数显示到文本框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26398272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display integers from array into textbox
提问by shaka
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.
我有一个名为 txtbox 的文本框,并且有一个名为 number 的数组中的数字,我需要在事件过程中将此数组中的数字显示到文本框中(用户将单击下一步,我必须显示下一个数字在数字数组中),我对 vb 还很陌生,到目前为止我所拥有的是。
Dim number() As Integer
Dim i As Integer
For i = 0 to number.Length -1
Me.txtbox.Text = number(i)
Next
回答by Tim Schmelter
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:
假设您的问题不是如何正确初始化数组,而是如何访问它以在 TextBox 中显示数字。您可以使用String.Join,例如:
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:
或通过 LINQ 扩展:
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.
如果您想从当前导航到下一个,您必须将当前索引存储在类的字段中,然后您可以在事件处理程序中增加/减少它。
回答by Shockwaver
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
回答by shaka
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

