Click here to Skip to main content
15,896,450 members
Home / Discussions / C#
   

C#

 
GeneralRe: Convert char* to string Pin
Heath Stewart12-May-04 3:57
protectorHeath Stewart12-May-04 3:57 
GeneralRe: Convert char* to string Pin
peraonline12-May-04 4:03
peraonline12-May-04 4:03 
GeneralCharacter Encoding Pin
gUrM33T11-May-04 1:45
gUrM33T11-May-04 1:45 
GeneralRe: Character Encoding Pin
Mike Dimmick11-May-04 3:33
Mike Dimmick11-May-04 3:33 
GeneralRe: Character Encoding Pin
Paul Watson11-May-04 4:11
sitebuilderPaul Watson11-May-04 4:11 
GeneralRe: Character Encoding Pin
Heath Stewart11-May-04 4:25
protectorHeath Stewart11-May-04 4:25 
GeneralbeginInvoke() Pin
sreejith ss nair11-May-04 1:32
sreejith ss nair11-May-04 1:32 
GeneralRe: beginInvoke() Pin
Heath Stewart11-May-04 4:14
protectorHeath Stewart11-May-04 4:14 
You don't need to dispose the labels - just remove them from the Controls collection property of your panel. If they are no longer being referenced (i.e., there's nothing else referencing them like any class fields or something), the GC will pick them up and dispose of them in a (usually) timely fashion.

If you aren't using fields to refer to these labels, then you'll have to iterate (not enumerate) through the Controls collection and find the label controls like so:
for (int i=0; i< Controls.Count; i++)
{
  Control c = Controls[i];
  if (c is Label) Controls.Remove(c);
  i--; // Don't increment the index since you've now shifted the collection
}
If you are using fields, you can do something like this:
Controls.Remove(myLabel1);
myLabel1 = new Label(); // Initialize...
Controls.Remove(nyLabel2);
myLabel2 = new Label(); // Initialize...
Notice that with this way, new label controls are created that the field refer to, meaning that they are no longer refering to the old labels. So long as nothing else is (and they were removed from the Controls collection, so it's not referencing them anymore either), they will be GC'd.

For non-component classes, calling Dispose is important, like instances of the Graphics class you create manually, or an Image or Bitmap. Controls are good about disposing themselves when necessary, but you could still call Dispose if you wanted to, like so:
Controls.Remove(myLabel1);
myLabel1.Dispose();
myLabel1 = new Label(); // Initialize...
Controls.Remove(myLabel2);
myLabel2.Dispose();
myLabel2 = new Label(); // Initialize...
All this does is free up a little more memory immediately, which would've been freed when necessary by the GC or when the GC got around to it.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: beginInvoke() Pin
Paul Watson11-May-04 4:14
sitebuilderPaul Watson11-May-04 4:14 
GeneralHTTP request,response Pin
dcronje11-May-04 1:05
dcronje11-May-04 1:05 
GeneralRe: HTTP request,response Pin
Heath Stewart11-May-04 4:05
protectorHeath Stewart11-May-04 4:05 
Generalhelp on wmi and registry Pin
chettu11-May-04 0:54
chettu11-May-04 0:54 
GeneralRe: help on wmi and registry Pin
Heath Stewart11-May-04 3:59
protectorHeath Stewart11-May-04 3:59 
GeneralRe: help on wmi and registry Pin
chettu11-May-04 20:18
chettu11-May-04 20:18 
GeneralRe: help on wmi and registry Pin
Heath Stewart12-May-04 2:52
protectorHeath Stewart12-May-04 2:52 
GeneralRe: help on wmi and registry Pin
chettu12-May-04 3:21
chettu12-May-04 3:21 
Generalblock the keyboard Pin
cristina_tudor11-May-04 0:36
cristina_tudor11-May-04 0:36 
GeneralRe: block the keyboard Pin
Corinna John11-May-04 0:50
Corinna John11-May-04 0:50 
GeneralReturning string from unmanaged dll Pin
Mikke_x10-May-04 23:50
Mikke_x10-May-04 23:50 
GeneralRe: Returning string from unmanaged dll Pin
Heath Stewart11-May-04 3:31
protectorHeath Stewart11-May-04 3:31 
GeneralRe: Returning string from unmanaged dll Pin
Mikke_x11-May-04 4:06
Mikke_x11-May-04 4:06 
GeneralRe: Returning string from unmanaged dll Pin
Heath Stewart11-May-04 4:59
protectorHeath Stewart11-May-04 4:59 
GeneralRe: Returning string from unmanaged dll Pin
Mikke_x11-May-04 5:02
Mikke_x11-May-04 5:02 
GeneralRe: Returning string from unmanaged dll Pin
Heath Stewart11-May-04 5:20
protectorHeath Stewart11-May-04 5:20 
QuestionFont to LOGFONT ? Pin
azusakt10-May-04 23:06
azusakt10-May-04 23:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.