-2

我在运行时动态添加文本框。以后如何在程序中访问它们?

4

1 回答 1

5

命名动态创建的控件

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim MyTextBox As New TextBox
    MyTextBox.Name = "MyTextBox"
    Me.Controls.Add(MyTextBox)
end sub

程序中的其他地方:

    'set text
    Me.Controls("MyTextBox").Text = "Hi there"
    'fetch text
    Dim thetext = Me.Controls("MyTextBox").Text

    'fetch textbox
    Dim tb As TextBox = CType(Me.Controls("MyTextBox"), TextBox)
    tb.Text = ""
    tb.BackColor = Color.Red

另一种方法是遍历 me.controlls-collection 并以这种方式找到控件(也许你已经在那些或其他东西上设置了 .tag="mycontrol" ......

于 2012-04-18T14:01:01.163 回答