Removing a collection of controls

18,763

Solution 1

Hiding the Panel first seems to make the controls disappear quicker than just clearing the Panel. See this code:

Option Strict On

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Panel1.Visible = False

        If Not Panel1.Controls.OfType(Of Button).Any() Then
            For x As Integer = 1 To 10
                For y As Integer = 1 To 10
                    Dim btn As New Button()
                    btn.Size = New Size(45, 45)
                    btn.Location = New Point((x - 1) * 45, (y - 1) * 45)
                    btn.Text = (x * y).ToString()
                    Panel1.Controls.Add(btn)
                    btn.Visible = True
                Next
            Next
        End If

        Panel1.Visible = True
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Panel1.Visible = False
        Panel1.Controls.Clear()
        Panel1.Visible = True
    End Sub
End Class

This code has 2 buttons, and a Panel. Button1 generates 100 buttons, places them on a Panel. Button2 hides the panel before removing them. Perhaps you can experiment with this idea.

Solution 2

It's not the deletion that tends to take the time - it's redrawing the form each time. Try surrounding your deletion code with calls to SuspendLayout and ResumeLayout

Private Sub ClearControls()
    'removing the controls from Me.Controls
    Me.SuspendLayout()
    For Each Control As Control In ListToDelete
        Me.Controls.Remove(Control)
    Next
    Me.ResumeLayout()
    ListToDelete = New List(Of Control)
End Sub

Solution 3

kindly never used remove and panel.removeat to remove any controls. It won't be able to delete last control in panel layout.Especially for panel.removeat will return out of index error once delete the last end control in panels. I'm also wondering need to know why this is problem appears?

Stored all the control name in string array, find those controls in panel and delete them, Try below code will help you delete all control in panel for one short. Try using with find and removeBykey function will make your task easier.

Dim ctrllist() as string
Dim counts = 0

For each control in Me.panel1.controls
redim Preserve ctrllist(0 to counts) 
ctrllist(counts)=control.name  
counts+=1
Next

For counts=Lbound(ctrllist) to Ubound(ctrlllist)
  If me.panel1.controls.find(ctrllist(counts),True).Length>0 then

     me.panel1.controls.removeBykey(ctrllist(counts))

  End If
Next

Hope it will help.

Solution 4

Put the controls in a panel container control. Removing the panel container removes all child controls.

Share:
18,763

Related videos on Youtube

Vella
Author by

Vella

I'm a developer in vb.net and C#. At this given time, I'm working on an application in vb.net. I'm still a student and learning by practice!

Updated on June 06, 2022

Comments

  • Vella
    Vella almost 2 years

    I have a form (Form1) and it has 30 controls. When I hit a button, I want to remove those 30 buttons and put other controls on the form. Now, my problem is that this is to slow.

    I have this list with controls I want to delete and I run through them with a For Each.

    Private Sub ClearControls()
        'removing the controls from Me.Controls
        For Each Control As Control In ListToDelete
            Me.Controls.Remove(Control)
        Next
        ListToDelete = New List(Of Control)
    End Sub
    

    Now, if you watch the form, you see the controls getting deleted 1 by 1. This action takes about 0.4 seconds (timed with the build-in stopwatch) and that's too long.

    Are there any solutions to delete the controls in a faster way or is it only possible to delete the controls 1 by 1?

    Maybe an important fact is that everything is connected with a database. The controls are created by a class I defined myself (TableDrawer) and it creates a rectangle or circle (depends on info from the database). I add the selfmade controls to the form and when I want to delete them, it takes 0.4 seconds to get other controls on the form - also with information out of my database.

    Hopefully this clears some things up and I hope you can help me out... It really has to go a bit faster (I hope to get 0.1s or lower)

Related