VB.net在按钮点击上添加多个控件

时间:2015-08-11 07:08:25

标签: vb.net controls

我试图创建一个根据请求生成自己的用户界面(按钮点击)

Private Sub Body_new_part_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Body_new_part_add.Click

因此,当我首先添加一个Combobox时,没问题就会生成盒子和盒子。把它放在正确的位置等。

    Dim oTypeBox As New ComboBox
    oTypeBox.Name = "Body_type_" & oBodyPartsNumber
    oTypeBox.Location = New System.Drawing.Point(7, 78)
    Body_parts.Controls.Add(oTypeBox)

现在我想添加另一个控件,即Combobox旁边的文本框。

    Dim oTypeBox As New ComboBox
    oTypeBox.Name = "Body_type_" & oBodyPartsNumber
    oTypeBox.Location = New System.Drawing.Point(7, 78)
    Body_parts.Controls.Add(oTypeBox)

    Dim oTextbox As New TextBox
    oTextbox.name = "test"
    oTextbox.Location = New System.Drawing.Point(50, 78)
    Body_parts.Controls.Add(oTextbox)

这给了我这个错误。

'New' cannot be used on an interface.

为了完成这项工作,我需要改变什么?我需要在每个按钮点击事件上添加+ - 10个控件。

1 个答案:

答案 0 :(得分:1)

试试这个

Public Class Form1
    Dim cLeft As Integer = 1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        AddNewTextBox()
    End Sub
    Public Function AddNewTextBox() As System.Windows.Forms.TextBox
        Dim txt As New System.Windows.Forms.TextBox()
        Me.Controls.Add(txt)
        txt.Top = cLeft * 25
        txt.Left = 100
        txt.Text = "TextBox " & Me.cLeft.ToString
        cLeft = cLeft + 1
        Return txt
    End Function
End Class