Solved

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

Posted on 2010-08-26
7
646 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now