如何将文本框输入存储到二维数组中?

时间:2017-04-18 19:26:28

标签: vb.net

好吧所以我有两个文本框供用户输入,我需要帮助将这些存储到一个二维数组中。 49列和两行(州,首都) 我已经将数组声明为:

Dim states(49,1) as string 
states(0,0)= textbox1.text
states(0,1) = textbox2.text
我不知道还有什么可以做,因为我有 我存储这个权利吗?我不知道还有什么可以将其余的输入存储到数组中。

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

声明模块/类范围变量:

Dim states(49,1) as string
Dim nextInd as Integer = 0

然后在你的按钮点击处理程序:

If nextInd <= 49 Then   ' Make sure you are not trying to fill values past the dimensions of the array

     states(nextInd, 0) = textbox1.text
     states(nextInd, 1) = textbox2.text

     nextInd += 1  ' To increment the next index to use by 1

     textbox1.text = ""
     textbox2.text = ""

End If

然后要显示数组的内容,需要一个循环:

    ' Use a string builder so you can modify the same string object to show it all together in the message box

    Dim contents As New StringBuilder("")

    For st = 0 To 49
        contents.Append(states(st, 0) & ": " & states(st, 1) & Environment.NewLine)
        ' Or however you want To format it
    Next

    MessageBox.Show(Me, contents)  ' MsgBox is old - use MessageBox instead