1

I'm trying to make a userinterface that generates itself on request (button click)

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

So when i add a Combobox as first it's no problem it generates the box & places it on the right position etc.

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

Now i want to add another control, a textbox next to the 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)

This gives me this error.

'New' cannot be used on an interface.

What do i need to change in order to get this done? I need to add +- 10 controls on each button click event.

1 Answer 1

2

Try this one

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
Sign up to request clarification or add additional context in comments.

4 Comments

but what is point of function(?) when You can put that code under button.
Functions are all about avoiding duplicating your code.
I know that and I agree... but in this particulary case (question) is unnecessarily.
Yes, but this is mine coding format that's why i used it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.