如何以编程方式向 VB.NET 中的窗体添加控件

问题描述:

我正在处理 Visual Basic 2010 Express Edition 中的清单.我不知道库存所需的字段数.我希望我可以在程序中使用 for 循环添加文本框/复选框/按钮.有没有办法在不使用工具箱的情况下向表单添加控件?

I am working on an inventory in Visual Basic 2010 Express Edition. I don't know the number of fields that will be necessary for the inventory. My hope was that I could add textboxes/checkboxes/buttons using for loops in the program. Is there a way to add controls to a form without using the toolbox?

我可以通过在程序中实例化控件来添加控件吗?

Can I add controls by instantiating them in the program?

是.

Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim MyTextbox as New Textbox
    With MyTextbox
       .Size = New Size(100,20)
       .Location = New Point(20,20)
    End With
    AddHandler MyTextbox.TextChanged, AddressOf MyTextbox_Changed
    Me.Controls.Add(MyTextbox)

'Without a help environment for an intelli sense substitution
'the address name and the methods name
'cannot be wrote in exchange for each other.
'Until an equality operation is prior for an exchange i have to work
'on an as is base substituted.

End Sub

Friend Sub MyTextbox_Changed(sender as Object, e as EventArgs)
   'Write code here.
End Sub