Solved

Clear the Runtime-Created Controls from a Panel vb.net

Posted on 2010-08-26
7
662 Views
Last Modified: 2012-06-27
How do I clear the a variety of controls I created inside of a Panel?

In side the panel, I load (at runtime) a series of text boxes, lables, radio buttons and checkboxes, when the user clicks on a ListView item.  if the user clicks on a different listview item, I need the contents of the panel cleared out - deleted, so that I can redraw the new controls that pertain to that listview item.

Hope this makes sense!

Thanks for your help!
0
Comment
Question by:slightlyoff
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33534024
Hello !

should be easy :

For each ctl as Controls in panel1.controls
    panel1.controls.remove(ctl)
next

Syldra
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33534171
Why not just?

    Panel1.Controls.Clear()

Done!
0
 
LVL 1

Author Closing Comment

by:slightlyoff
ID: 33534305
Thanks!  That worked perfectly.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33534351
Sorry for not responding earlier, but there's an important fact you should know : The Clear method does not remove control handles from memory, that may lead to memory leaks. That's why I use the Remove method.

Simpler does not always mean better...

Syldra
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33534517
"The Clear method does not remove control handles from memory"

What?....

The controls that are removed will no longer have any references to them (assuming none were explicitly stored somewhere else) and they will AUTOMATICALLY be GARBAGE COLLECTED.

.Net has always done this...where are you getting your information from Syldra?
0
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33534707
Is MSDN a good enough source ?

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.clear(v=VS.100).aspx

It's been like this since .Net 2.0... you have to call the Dispose method manually.

That's what I understand... I might be wrong.

But as they say, I'd rather be safe than sorry.

Syldra
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33535167
That's funny how the documentation states that you have to call Dispose() on the controls in the collection but then doesn't do it in the example provided right there on MSDN...  =\

The Remove() method probably doesn't dispose of a control either though...what if you just wanted to remove it but add it back in later?

I just tried both methods and couldn't see any decrease or difference in memory usage, handle count, or gdi object count between the two.

I even tried this:

        While Panel1.Controls.Count > 0
            Panel1.Controls(0).Dispose()
        End While
        GC.Collect()
        GC.WaitForPendingFinalizers()

...and the memory usage dropped slightly but the handle count and gdi object count held steady...but there was also no increase in count for these values when the dynamic controls were created and added to the form.

Thanks for pointing out the documentation but I can't see any difference...
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 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