disposing controls on panel

  • Thread starter Thread starter David
  • Start date Start date
D

David

hello...
i want to renove all controls from panel...
i use this:

foreach (Control c in panel.Controls)
panel.Controls.Remove(c);

but it's doesn't work...
-- not all controls are removed...

thanx
 
David,

It should remove them. Can you show an example of where they are not
removed?

It works for me. I use RemoveAt and a for loop, and not a foreach,
because you can't modify the collection while enumerating through it.

Also, you probably don't see an exception because you do this in an
event handler for a button, and the exception gets swallowed. Do this
instead:

// Remove all controls.
for (int index = panel.Controls.Count - 1; index >= 0; index--)
// Remove the controls.
panel.Controls.RemoveAt(index);

Hope this helps.
 
David said:
i want to renove all controls from panel...
i use this:

foreach (Control c in panel.Controls)
panel.Controls.Remove(c);

but it's doesn't work...
-- not all controls are removed...

I suspect the problem is that you're iterating through a collection and
changing it during the iteration. That's not a good idea. Try this
instead:

ArrayList list = new ArrayList (panel.Controls);
foreach (Control c in list)
{
panel.Controls.Remove(c);
}
 
Is there a difference if you draw the controls manually into the project
versus define them at runtime?

I mean, if you manually draw controls but then at runtime try to remove
them, is it possible to somehow miss a handle/definition? Whereas if you
draw them at runtime, the handle works better to remove them?
 
Bradley1234 said:
Is there a difference if you draw the controls manually into the project
versus define them at runtime?

I mean, if you manually draw controls but then at runtime try to remove
them, is it possible to somehow miss a handle/definition? Whereas if you
draw them at runtime, the handle works better to remove them?

I'm not really sure what you mean - the only time you *can* draw them
is at runtime...
 
Jon Skeet said:
I'm not really sure what you mean - the only time you *can* draw them
is at runtime...

You can either manually create them at "design time" by drag/dropping of the
items from the tools tab, sizing them, etc; or you can define controls on
the fly from your program.

So what I meant was if you defined them on the fly, the handle to control
them might work; whereas if they were defined at design time?? something
might get missed
 
Bradley1234 said:
You can either manually create them at "design time" by drag/dropping of the
items from the tools tab, sizing them, etc; or you can define controls on
the fly from your program.

There's nothing magic about creating them at design time - it's not
like the controls actually exist in the program. They're created in
code in the same way as if you manually write the code to do it.
So what I meant was if you defined them on the fly, the handle to control
them might work; whereas if they were defined at design time?? something
might get missed

I think your impression of design time is slightly off - it's really
just VS.NET writing some code for you (and putting some values into
resource files).
 
Jon Skeet said:
There's nothing magic about creating them at design time - it's not
like the controls actually exist in the program. They're created in
code in the same way as if you manually write the code to do it.

Yes, well obviously but I think the original problem was not being able to
hide all of them;


I think your impression of design time is slightly off - it's really
just VS.NET writing some code for you (and putting some values into
resource files).

My impression of design time? Im talking about coding and that a person had
trouble managing some controls on a form, my point is that if you draw them
at design time you could change default attributes and forget; If you
design them at runtime there is less tampering with the default attributes
and/or the handle to modify them, less chance of human error. No?

Im not confused about the little people who live in the machine and draw
them somehow on my computer, I mean thats the only way it makes sense
 
Bradley1234 said:
Yes, well obviously but I think the original problem was not being able to
hide all of them;

My impression of design time? Im talking about coding and that a person had
trouble managing some controls on a form, my point is that if you draw them
at design time you could change default attributes and forget; If you
design them at runtime there is less tampering with the default attributes
and/or the handle to modify them, less chance of human error. No?

That much is true, yes. That's not the kind of difference you seemed to
be implying earlier in the thread though - you seemed to think there
was some kind of fundamental difference between the two ways of
creating controls.
 
Jon Skeet said:
That much is true, yes. That's not the kind of difference you seemed to
be implying earlier in the thread though - you seemed to think there
was some kind of fundamental difference between the two ways of
creating controls.

Right, thanks. I guess this shows that its sometimes difficult to express
the full scope of meanings in a few sentences, we were talking apples and
oranges, since it was forms design vs on the fly but you said they are only
drawn at runtime. Both are true.

Im just happy to be using C#, thats what matters.
 
Back
Top