VB使用变量名来访问控制属性



我正在尝试使用变量名称设置动态创建的文本框的Text属性,但是当我使用控制(变量名)。文本,我得到一个错误,说我需要设置为"新"。使用变量的文本框的name属性在创建时设置,但我似乎无法使用相同的名称检索。

    Private Sub Example(panposition As Integer)
    Dim tbfile = New TextBox()
    Dim lineExample As Integer = 2
    ' creating a text box with a variable name
    Controls.Add(tbfile)                    ' create the new textbox to hold the file name
    tbfile.Name = "tbfile" + panposition.ToString
    tbfile.Location = New Point(85, tvposition)
    tbfile.Size = New Size(155, 20)
    tbfile.Text = "file name"
    tbfile.TextAlign = HorizontalAlignment.Left
    tbfile.HideSelection = False
    tbfile.TabStop = False
    tbfile.AllowDrop = False
    tbfile.Visible = True
    ' trying to update the text in the text box using file name and text retrieved from an array
    Me.Controls.(arrTextVals(1, lineExample)).Text = arrTextVals(2, lineExample)

End Sub

我认为问题出在行:

Me.Controls.(arrTextVals(1, lineExample)).Text = arrTextVals(2, lineExample)

以这种方式处理控件的正确方法是创建一个像这样的引用

Me.Controls(i).Text = arrTextVals(2, lineExample)

,其中I是一个整数,或者使用所需控件的名称,在您的情况下可以是

Me.Controls(arrTextVals(1, lineExample)).Text = arrTextVals(2, lineExample)

当然我想你之前提到的,arrTextVals是一个字符串数组

编辑:

在Me.Controls后面有一个点。(<-永远不要放。

最新更新