Controls left behind on a resize

J

james

As part of my app I am programatically adding some Label controls to a form
at runtime. The code for these is part of a function that is overrriding the
Paint event of the form. The other stuff that is drawn in the same function
works fine when I resize the screen (some lines and so on) but the labels
remain.

I tried deleting them by putting the following in the forms' resize event
(the labels are the only "controls" on the form):

for (int c = 0; c < this.Controls.Count; c++)
{
this.Controls.RemoveAt(c);
}

and it sort of works, but I still get some labels left behind - not the
originals but one or two stragglers from when it's resized.
I then tried a putting a "this.refresh()" in there too, but the form just
went into a mad loop when I tried to refresh it and I had to kill the
process :)

Any suggestions as to where I'm going wrong?
James.
 
B

Bruce Wood

james said:
As part of my app I am programatically adding some Label controls to a form
at runtime. The code for these is part of a function that is overrriding the
Paint event of the form. The other stuff that is drawn in the same function
works fine when I resize the screen (some lines and so on) but the labels
remain.

I tried deleting them by putting the following in the forms' resize event
(the labels are the only "controls" on the form):

for (int c = 0; c < this.Controls.Count; c++)
{
this.Controls.RemoveAt(c);
}

and it sort of works, but I still get some labels left behind - not the
originals but one or two stragglers from when it's resized.
I then tried a putting a "this.refresh()" in there too, but the form just
went into a mad loop when I tried to refresh it and I had to kill the
process :)

Any suggestions as to where I'm going wrong?

Well, I'm not sure that I understand exactly what it is you're seeing,
but the code you posted is wrong. Consider the case in which you have
three labels on your form. Look at what the loop will do:

c = 0
this.Controls.RemoveAt(0); // Removes first label.
// Second label is now at index 0, third label is at index 1.
c = 1
// Loop repeats because this.Controls.Count is now 2.
this.Controls.RemoveAt(1); // Removes what was third label.
// What was second label is still at index 0.
c = 2
// Loop terminates because Controls.Count is now 1,
// leaving what was originally the second label as the only
// control left in the collection.

In cases like this, you need to loop _backward_ through the controls,
so that the control you remove doesn't affect the "numbering" of the
other controls:

for (int c = this.Controls.Count - 1; c >= 0; c--)
{
this.Controls.RemoveAt(c);
}

will clear all of the controls from the collection.
 
J

james

Bruce Wood said:
In cases like this, you need to loop _backward_ through the controls,
so that the control you remove doesn't affect the "numbering" of the
other controls:

for (int c = this.Controls.Count - 1; c >= 0; c--)
{
this.Controls.RemoveAt(c);
}

will clear all of the controls from the collection.

Many thanks Bruce, the explanation made complete sense, and the code snippet
works a treat.
James.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top