如何将文本框值保存到数组中?

时间:2012-07-23 12:30:11

标签: vb6

我需要在文本框中输入值,当我按下回车键时,我必须将文本框值保存到数组中。如何才能实现此功能?

4 个答案:

答案 0 :(得分:1)

如果您只想在每次按下按钮时添加新条目,请使用以下内容:

Redim Preserve YourArray(LBound(YourArray) To UBound(YourArray) + 1)
YourArray(UBound(YourArray)) = TextBox.Text

请注意,当数组包含大量项目时,这会变得非常慢且效率低,因为每次重新分配内存。 更好的方法是在块中扩展数组的大小,同时跟踪最后一个有效的条目。

答案 1 :(得分:1)

我会使用一个单独的计数器变量来重新定义数组的大小,如下所示:

Option Explicit

Dim myArr() As String  '~~~ dynamic array
Dim lngCnt As Long     '~~~ a counter variable that keep track of the index

' inital stage..
Private Sub Form_Load()
    lngCnt = 0
End Sub

' on KeyPress
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then   '~~~ if Enter Key is pressed..
        ReDim Preserve myArr(lngCnt)    '~~~ reclare the array with the new size, while preserving any elements it may contain
        myArr(lngCnt) = Text1.Text      '~~~ store the line
        Text1.Text = ""     '~~~ empty the textbox, so that you could type the next line

        lngCnt = lngCnt + 1 '~~~ increment the counter, which we would use as size during the next keypress
    End If
End Sub

' to display the elements
Private Sub Command1_Click()
    Dim i As Long

    For i = LBound(myArr) To UBound(myArr)  '~~~ loop through the elements(from Lowerbound to Upperbound)..
        Debug.Print myArr(i)                '~~~ ..and display the item.
    Next
End Sub

答案 2 :(得分:0)

只要为所有文本框提供相同的名称

答案 3 :(得分:0)

简短的回答是使用split来破坏字符串(您需要告诉用户要分割的字符串。)

长答案不会将用户界面更改为转发器并让它们使用每个值的输入从此处开始http://blogs.microsoft.co.il/blogs/basil/archive/2008/08/20/javascript-repeater-control-datarepeater-using-jquery-presenter-1-0-8-uicontrols-library.aspx

否则你将永远调试。

相关问题