簡體   English   中英

VB使用變量名訪問控件屬性

[英]VB Using variable name to access control properties

我正在嘗試使用變量名設置動態創建的文本框的Text屬性,但是當我使用Me.Controls(變量名).Text時,出現錯誤消息,我需要將其設置為“ New ”。 文本框的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)

其中我是整數或使用所需控件的名稱,在您的情況下可能是

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

當然,我想正如您之前提到的,arrTextVals是一個字符串數組

編輯:

您在Me.Controls后面有一個點。(<-切勿在方括號前加.。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM