0

在表单中,每次我单击 te 按钮时,都会出现一个新的文本框。我想用所有这些文本框制作一个数组。问题是,所有这些文本框都有一个动态名称。我如何将它们放入数组中?这是我的代码:

Set nieuwtxtingredient = Me.Controls.Add("Forms.Textbox.1", "Ingredient", True)
With nieuwtxtingredient
.Width = Me.txtIngredient0.Width
.Height = Me.txtIngredient0.Height
.Left = Me.txtIngredient0.Left
.Top = Me.txtIngredient0.Top + 30 * aantalBoxes
.Name = "txtIngredient" + CStr(aantalBoxes)
End With

Dim naam As String
Dim ingredientArray() As String

ReDim ingredientArray(1 To aantalBoxes)
ingredientArray(aantalBoxes) = **Me.txtIngredient0.Value**
4

2 回答 2

0

在您的代码中,您将对新文本框的引用保存在nieuwtxtingredient.

您可以将此引用保存在一个文本框数组中,然后读取每个文本框的名称和值。

这就是我建议修改代码的方式:

Dim aantalBoxes As Integer
Dim ingredientArray() As Control

 Private Sub btnVoegToe_Click()

    Dim aantalRecepten As Integer
    Dim Teller As Integer

    aantalRecepten = Cells(2, Columns.Count).End(xlToLeft).Column

    Cells(2, aantalRecepten + 2).Value = Me.txtNaamRecept.Value

    For Teller = 1 To aantalBoxes ' <-- starts at 1, formula below adjusted

        Cells(2 + Teller, aantalRecepten + 2).Value = ingredientArray(aantalBoxes).Value

    Next Teller

End Sub

Private Sub btnVolgendeIngredient_Click()

    aantalBoxes=aantalBoxes + 1     ' <-- must calculate the total

    ReDim Preserve ingredientArray(aantalBoxes) ' <-- you had this in the earlier version

    Set nieuwtxtingredient = Me.Controls.Add("Forms.Textbox.1", "Ingredient", True)

    With nieuwtxtingredient

        .Width = Me.txtIngredient0.Width
        .Height = Me.txtIngredient0.Height
        .Left = Me.txtIngredient0.Left
        .Top = Me.txtIngredient0.Top + 30 * aantalBoxes
        .Name = "txtIngredient" + CStr(aantalBoxes)

    End With
                                       ' first Textbox is numbered 1
    set ingredientArray(aantalBoxes) = nieuwtxtingredient  ' <-- you had this in the earlier version

End Sub
于 2013-04-03T19:41:31.543 回答
0

见这个例子,http://jsfiddle.net/7zkzttpr/2/

$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});   

当你创建一个新的文本框时,给文本框命名为一个数组,这样你就可以得到一个数组中所有文本框的值。

于 2014-10-24T14:16:53.260 回答