Clear the Runtime-Created Controls from a Panel

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!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hello !

should be easy :

For each ctl as Controls in panel1.controls

Mike TomlinsonMiddle School Assistant TeacherCommented:
Why not just?



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slightlyoffAuthor Commented:
Thanks!  That worked perfectly.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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...

Mike TomlinsonMiddle School Assistant TeacherCommented:
"The Clear method does not remove control handles from memory"


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?
Is MSDN a good enough source ?

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.

Mike TomlinsonMiddle School Assistant TeacherCommented:
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
        End While

...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...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.