Click here to Skip to main content
16,022,536 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
add button is correctly work But reset button Not Work (Always Keep one Panel)

VB
Public Class Form1

    Private Sub clear(ByVal i As Control)
        Dim frmControl As Control
        For Each frmControl In i.Controls
            If TypeOf frmControl Is Panel Then
                frmControl.Dispose()
            End If
        Next
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
        Static wc As SByte = 1

        Dim P As New Panel
        P.Name = "Panel" & WC.ToString
        P.Size = New System.Drawing.Size(144, 51)
        FlowLayoutPanel1.Controls.Add(P)
        

        'Add TextBox
        Dim tb As New TextBox
        tb.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        tb.Name = "TextBox" & WC.ToString
        tb.Dock = DockStyle.Top
        tb.AutoSize = True
        P.Controls.Add(tb)
        tb.Focus()

        'Add Play Button 1
        Dim b1 As New Button
        b1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        b1.Name = "ButtonPlay" & WC.ToString
        b1.Size = New System.Drawing.Size(75, 23)
        b1.Location = New System.Drawing.Point(70, 23)
        b1.Text = "پخش"
        Me.AddButton.UseVisualStyleBackColor = True
        P.Controls.Add(b1)

        'Add Delete Button 2
        Dim b2 As New Button
        b2.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        b2.Name = "ButtonDelete" & WC.ToString
        b2.Size = New System.Drawing.Size(40, 23)
        b2.Location = New System.Drawing.Point(30, 23)
        b2.Text = "حذف"
        Me.AddButton.UseVisualStyleBackColor = True
        P.Controls.Add(b2)

        'Add Insert Button 3
        Dim b3 As New Button
        b3.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        b3.Name = "ButtonInsert" & WC.ToString
        b3.Size = New System.Drawing.Size(30, 23)
        b3.Location = New System.Drawing.Point(0, 23)
        b3.Text = "+"
        Me.AddButton.UseVisualStyleBackColor = True
        P.Controls.Add(b3)
        wc += 1

        AddButton.SendToBack()
        Button2c.SendToBack()

    End Sub


    Private Sub Button2c_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2c.Click
        clear(FlowLayoutPanel1)
    End Sub
End Class
Posted

1 solution

You shouldn't do it that way - you haven't removed the controls from the Controls array of the flow layout panel, just Disposed them.
VB
Private Sub clear(i As Control)
    While i.Controls.Count > 0
        Dim c As Control = i.Controls(0)
        If TypeOf c Is Panel Then
            i.Controls.Remove(c)
            c.Dispose()
        End If
    End While
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900