2015-09-08 62 views
1

我想設置一個動態創建的文本框使用變量名的Text屬性,但是當我使用Me.Controls(變量名).Text,我得到一個錯誤說我需要將其設置爲「新建」。使用變量的文本框的名稱屬性在創建時已設置,但我似乎無法使用相同的名稱進行檢索。VB使用變量名稱來訪問控件屬性

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 
+0

你從哪裏得到錯誤?我無法看到New TextBOx的名稱添加到'arrTextVals'的位置。列表或可能是字典(取決於其中的其他信息),可能會更好地工作 – Plutonix

+0

嘗試使用DirectCast來獲取控件 – Dman

+0

嘗試在設置屬性後添加控件。 – rheitzman

回答

1

我認爲這個問題是在行:

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後一個點(< - 從來沒有把一個。在括號之前。