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

Mar*_*eil 13 vb.net

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

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

Hol*_*ndt 18

是.

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 Size(20,20)
    End With
    AddHandler MyTextbox.TextChanged, AddressOf MyTextbox_TextChanged
    Me.Controls.Add(MyTextbox)  
End Sub

Friend Sub MyTextbox_Changed(sender as Object, e as EventArgs)
   'Write code here.
End Sub
Run Code Online (Sandbox Code Playgroud)

  • 请添加一些解释. (3认同)

Tha*_*lia 8

Dim numberOfButtons As Integer
Dim buttons() as Button

Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Redim buttons(numberOfbuttons)
    for counter as integer = 0 to numberOfbuttons
        With buttons(counter)
           .Size = (10, 10)
           .Visible = False
           .Location = (55, 33 + counter*13)
           .Text = "Button "+(counter+1).ToString ' or some name from an array you pass from main
           'any other property
        End With
        '
    next
End Sub
Run Code Online (Sandbox Code Playgroud)

如果要检查哪些文本框具有信息,或者单击了哪个单选按钮,则可以在"确定"按钮中循环遍历循环.

如果您希望能够单击各个数组项并让它们响应事件,请在Form_load循环中添加以下内容:

AddHandler buttons(counter).Clicked AddressOf All_Buttons_Clicked 
Run Code Online (Sandbox Code Playgroud)

然后创造

Private Sub All_Buttons_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
     'some code here, can check to see which checkbox was changed, which button was clicked, by number or text
End Sub
Run Code Online (Sandbox Code Playgroud)

你打电话的时候: objectYouCall.numberOfButtons = initial_value_from_main_program

response_yes_or_no_or_other = objectYouCall.ShowDialog()
Run Code Online (Sandbox Code Playgroud)

对于单选按钮,文本框,相同的故事,不同的结尾.


小智 6

Public Class Form1
    Private boxes(5) As TextBox

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim newbox As TextBox
        For i As Integer = 1 To 5 'Create a new textbox and set its properties26.27.
        newbox = New TextBox
        newbox.Size = New Drawing.Size(100, 20)
        newbox.Location = New Point(10, 10 + 25 * (i - 1))
        newbox.Name = "TextBox" & i
        newbox.Text = newbox.Name   'Connect it to a handler, save a reference to the array & add it to the form control.
        AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
        boxes(i) = newbox
        Me.Controls.Add(newbox)
        Next
    End Sub

    Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs)
        'When you modify the contents of any textbox, the name of that textbox
        'and its current contents will be displayed in the title bar

        Dim box As TextBox = DirectCast(sender, TextBox)
        Me.Text = box.Name & ": " & box.Text
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)