0

テキスト ボックスに値を入力する必要があり、Enter キーを押すたびに、テキスト ボックスの値を配列に保存する必要があります。この機能を実現するにはどうすればよいですか?

4

4 に答える 4

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
于 2012-07-23T15:41:18.670 に答える
1

ボタンを押すたびに新しいエントリを追加したい場合は、次のようなものを使用します。

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

配列に多数のアイテムが含まれていると、毎回メモリが再割り当てされるため、これは非常に遅くなり、非効率になる可能性があることに注意してください。より良い方法は、最後の有効なエントリを追跡しながら、配列のサイズをチャンクで拡張することです。

于 2012-07-23T13:00:19.750 に答える
0

すべてのテキストボックスに同じ名前を付けるだけです

于 2012-07-23T12:34:49.227 に答える
0

簡単な答えは、分割を使用して文字列を分割することです (分割に使用する文字をユーザーに伝える必要があります)。

長い答えは、ユーザーインターフェイスをリピーターに変更せず、値ごとに入力を使用するようにしますhttp://blogs.microsoft.co.il/blogs/basil/archive/2008/08/20/javascript-repeater- jquery-presenter-1-0-8-uicontrols-library.aspx を使用した制御データリピーター

そうしないと、永遠にデバッグすることになります。

于 2012-07-23T12:37:32.477 に答える