By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

Controls left behind on a resize

P: n/a
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.
Sep 25 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

james wrote:
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.

Sep 25 '06 #2

P: n/a

"Bruce Wood" <br*******@canada.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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.
Sep 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.