Solved

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

Posted on 2010-08-26
7
668 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 86

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 86

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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