[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I remove/dispose all controls in a flow layout panel visual studio 2008?

Posted on 2009-04-17
7
Medium Priority
?
2,969 Views
Last Modified: 2013-11-26
Experts this is really baffling me.  I have a visual studio 2008 windows form.  When the form loads I dynamically build 17 panels, I also add controls to those same panels, finally I add those panels to a flow layout panel.  The reasoning behind this, is the 17 panels represents a 1 record in our database, I will not know the details of the panel until the query is ran.  When the record is completed via database side I need to remove those 17 panels and eventually build 17 more panels.  If you look at the code below I thought i could just loop through all the controls in the panel and dispose them.  This is not the case not all the controls get disposed.  When I click the button only 9 get disposed, then the second click 4 get disposed, then 2, then 1, then 1.  Totalling 17 controls in all.  I need to click the button 5 times to completely dispose all the controls, why is this and how come the don't get diposed the first time through?  If you need the code showing how I dynamically build the panels let me know.
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        clear_controls(Me.flpbase) 'Flow layout panel containing controls I want to dispose
    End Sub
 
    Sub clear_controls(ByVal ctrl As Control)
        Dim frmctrl As Control
        For Each frmctrl In ctrl.Controls
             DirectCast(frmctrl, Panel).Dispose()
        Next
    End Sub

Open in new window

0
Comment
Question by:tentavarious
  • 5
7 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24168004
The for-each calls an enumerator. When you dispose a control, the enumerator will move one control upwards (because you kicked one out in the middle of the loop). This is why enumerators are not handy to use when inside the loop you are changing the list the enumerator is walking over.

To change this behavior, do the reverse. Use an old-school for-loop that walks backwards from the top and you will see that it actually does work.
0
 
LVL 39

Expert Comment

by:abel
ID: 24168030
Like this:

' walking the list backwards '
For iIdx As Integer = Me.Controls.Count To 0 Step -1
    If TypeOf Me.Controls(iIdx) Is Panel Then
        CType(Me.Controls(iIdx), Panel).Dispose()
    End If
Next

Open in new window

0
 
LVL 39

Accepted Solution

by:
abel earned 1400 total points
ID: 24168068
Apologies, minor bug, change it to this (see the " - 1 " with the Count)

' walking the list backwards '
For iIdx As Integer = Me.Controls.Count - 1 To 0 Step -1
    If TypeOf Me.Controls(iIdx) Is Panel Then
        CType(Me.Controls(iIdx), Panel).Dispose()
    End If
Next

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:tentavarious
ID: 31571462
Thanks that solved it
0
 
LVL 39

Expert Comment

by:abel
ID: 24168560
> Thanks that solved it

glad it helped :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24168621
Another option:

        Dim ctls As New List(Of Control)
        ctls.AddRange(Me.flpbase.Controls.OfType(Of Panel).ToArray)
        For Each ctl As Control In ctls
            ctl.Dispose()
        Next
0
 
LVL 39

Expert Comment

by:abel
ID: 24168796
@Idle_Mind: nice alternative!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question