简体   繁体   English

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

[英]How to store textbox input into a two dimensional array?

okay so i have two textboxes for user input, and i need help storing these into a single two dimensional array. 好吧,所以我有两个文本框供用户输入,我需要帮助将它们存储到单个二维数组中。 for 49 columns and two rows (states, capitals) i already declared the array to: 对于49列和两行(州,大写),我已经声明了以下数组:

Dim states(49,1) as string 
states(0,0)= textbox1.text
states(0,1) = textbox2.text

im not sure what else to do because i have am i storing this right? 即时通讯不知道该怎么办,因为我有正确的权限吗? im not sure what more to do to store the rest of input into the array. 我不知道该怎么做才能将其余的输入存储到数组中。

any help would be appreciated. 任何帮助,将不胜感激。 thank you! 谢谢!

Declare module/class scope variables: 声明模块/类范围变量:

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

Then in your button click handler: 然后在您的按钮中单击处理程序:

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

And then to display the contents of the array, you need a loop: 然后要显示数组的内容,您需要一个循环:

    ' 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM