This is my code to create textboxs with button.
Now I need a button which to delete all the text box.
What is the way of doing it ??
AND
can I write the code below as a class and I just call the class in the button only??

For Me.count = count To 5


            Dim label1 = New Label
            With label1
                .Name = "lblNo" & count
                .Size = New Size(32, 26)
                .Location = New Point(x, y)
                '.Font = txtNum1.Font
                .TextAlign = HorizontalAlignment.Center
                .Text = count & "."
                .TabStop = False
                Controls.Add(label1)
            End With

            x += 53

            Dim myTextBox2 = New TextBox
            With myTextBox2
                .Name = "txtNum" & count
                .Size = New Size(50, 26)
                .Location = New Point(x, y)
                ' .Font = txtNum1.Font
                .TextAlign = HorizontalAlignment.Center
                .MaxLength = 4
                Controls.Add(myTextBox2)
            End With

            x += 75

            Dim myTextBox3 = New TextBox
            With myTextBox3
                .Name = "txtBig" & count
                .Size = New Size(50, 26)
                .Location = New Point(x, y)
                '  .Font = txtNum1.Font
                .TextAlign = HorizontalAlignment.Center
                .MaxLength = 6
                Controls.Add(myTextBox3)
            End With

            x += 70

            Dim myTextBox4 = New TextBox
            With myTextBox4
                .Name = "txtSmall" & count
                .Size = New Size(50, 26)
                .Location = New Point(x, y)
                ' .Font = txtNum1.Font
                .TextAlign = HorizontalAlignment.Center
                .MaxLength = 6
                Controls.Add(myTextBox4)
            End With
            x = 12
            y += 25
        Next
        If count >= 11 Then
            MessageBox.Show("Maximum rows are 10", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If

Recommended Answers

All 3 Replies

To delete a control on the form you can do

Me.Controls.Remove(controlname)
controlname = Nothing

So if you want to remove a button named (for example) btnAdd just do

Me.Controls.Remove(btnAdd)
btnAdd = Nothing

To remove all controls of a given type (eg TextBox) do

For Each ctrl As Control In Me.Controls
    If TypeOf ctrl Is TextBox Then
        Me.Controls.Remove(ctrl)
        ctrl = Nothing
    End If
Next

To delete a control on the form you can do

Me.Controls.Remove(controlname)
controlname = Nothing

So if you want to remove a button named (for example) btnAdd just do

Me.Controls.Remove(btnAdd)
btnAdd = Nothing

To remove all controls of a given type (eg TextBox) do

For Each ctrl As Control In Me.Controls
    If TypeOf ctrl Is TextBox Then
        Me.Controls.Remove(ctrl)
        ctrl = Nothing
    End If
Next

I try your code and its work.
But after I delete the textbox,I cant recreate the textbox by using mycode.
why?

You used Controls.Add(myTextBox#) where you should have used Me.Controls.Add(myTextBox#). If you want to remove then readd them, why don't you just use the Visible property to show/hide them instead? Also, if you create them via code then you will have to use AddHandler if you want to be able to handle events. There are several examples on this site of creating controls dynamically in this fashion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.