tags:

views:

173

answers:

3

Hi

I am dynamically displaying array of labels on a form and am getting a new set of labels to be displayed on the form when a function is called again. But instead, the previous labels are still on the screen with the new labels. How do I clear the previous set of labels on the form?

Thanks

A: 

You need to remove the old labels from the Form's "Controls" collection. Is this a good idea/design? Not so sure, but without seeing any code this is the best advice I can offer.

Ed Swangren
thank you so much
Arvind
+1  A: 

Check out this article on "Implementing a Remove Method". You need Controls.Remove.

SwDevMan81
thank you so much
Arvind
A: 

When you add the first set of labels to your form's Controls collection, also add them to a form-level List (call it "_labels" or something). When you want to remove this first set from the form, do something like this:

foreach (Label l in _labels)
{
    this.Controls.Remove(l);
}
_labels.Clear();

Or if the only controls you have on the form are labels, you can remove them more simply:

this.Controls.Clear();
MusiGenesis
thank you so much
Arvind