【问题标题】:How to programmatically add controls to a form in VB.NET如何以编程方式将控件添加到 VB.NET 中的窗体
【发布时间】:2012-07-03 23:07:07
【问题描述】:

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

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

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    是的。

    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
    

    【讨论】:

    • 请添加一些解释。
    • 嗨,Holger,我可以在面板上添加这个吗?
    • 是的,你可以。每个控件都有一个 Controls 属性,您可以从中添加(或删除)控件。只需记住手动添加您想要实现的任何处理程序。对于您的面板,将上面示例中的“MyForm”替换为您的面板名称。
    【解决方案2】:

    您可以获取 Button1 的位置,然后每次单击它时增加 Y 值。

    Public Class Form1
        Dim BtnCoordinate As Point
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim btn As Button = New Button
            BtnCoordinate.Y += Button1.Location.Y + 4
            With btn
                .Location = New Point(BtnCoordinate)
                .Text = TextBox1.Text
                .ForeColor = Color.Black
            End With
            Me.Controls.Add(btn)
            Me.StartPosition = FormStartPosition.CenterScreen
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Button1Coordinate = Button1.Location
        End Sub
    End Class
    

    【讨论】:

      【解决方案3】:
      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
      

      【讨论】:

        【解决方案4】:

        要向表单动态添加控件,请执行以下代码。在这里,我们正在创建文本框控件以动态添加。

        Public Class Form1
            Private m_TextBoxes() As TextBox = {}
        
            Private Sub Button1_Click(ByVal sender As System.Object, _
                                      ByVal e As System.EventArgs) _
                                      Handles Button1.Click
        
                ' Get the index for the new control.
                Dim i As Integer = m_TextBoxes.Length
        
                ' Make room.
                ReDim Preserve m_TextBoxes(i)
        
                ' Create and initialize the control.
                m_TextBoxes(i) = New TextBox
                With m_TextBoxes(i)
                    .Name = "TextBox" & i.ToString()
                    If m_TextBoxes.Length < 2 Then
                        ' Position the first one.
                        .SetBounds(8, 8, 100, 20)
                    Else
                        ' Position subsequent controls.
                        .Left = m_TextBoxes(i - 1).Left
                        .Top = m_TextBoxes(i - 1).Top + m_TextBoxes(i - _
                            1).Height + 4
                        .Size = m_TextBoxes(i - 1).Size
                    End If
        
                    ' Save the control's index in the Tag property.
                    ' (Or you can get this from the Name.)
                    .Tag = i
                End With
        
                ' Give the control an event handler.
                AddHandler m_TextBoxes(i).TextChanged, AddressOf TextBox_TextChanged
        
                ' Add the control to the form.
                Me.Controls.Add(m_TextBoxes(i))
            End Sub
        
            'When you enter text in one of the TextBoxes, the TextBox_TextChanged event
            'handler displays the control's name and its current text.
            Private Sub TextBox_TextChanged(ByVal sender As  _
            System.Object, ByVal e As System.EventArgs)
                ' Display the current text.
                Dim txt As TextBox = DirectCast(sender, TextBox)
                Debug.WriteLine(txt.Name & ": [" & txt.Text & "]")
            End Sub
        End Class
        

        【讨论】:

        • 如果表单允许,我会将这些添加到FlowLayoutPanel 或动态添加单元格到DataGridView。这样就无需设置边界或确切位置。很好的例子。
        【解决方案5】:
        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
        

        如果您想检查哪些文本框有信息,或者单击了哪个单选按钮,您可以在 OK 按钮中循环遍历。

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

        AddHandler buttons(counter).Clicked AddressOf All_Buttons_Clicked 
        

        然后创建

        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
        

        当您致电时:objectYouCall.numberOfButtons = initial_value_from_main_program

        response_yes_or_no_or_other = objectYouCall.ShowDialog()
        

        对于单选按钮、文本框、相同的故事、不同的结局。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-23
          • 1970-01-01
          • 2015-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-30
          • 2011-02-18
          相关资源
          最近更新 更多