473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

disposing controls on panel

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

Nov 16 '05 #1
9 14344
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David" <da*******@hotmail.com> wrote in message
news:u$**************@TK2MSFTNGP11.phx.gbl...
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


Nov 16 '05 #2
David <da*******@hotmail.com> wrote:
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);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
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?
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
David <da*******@hotmail.com> wrote:
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);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
Bradley1234 <so*****@yahoo.com> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bradley1234 <so*****@yahoo.com> wrote:
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...


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
Nov 16 '05 #6
Bradley1234 <so*****@yahoo.com> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bradley1234 <so*****@yahoo.com> wrote:
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.


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;
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).


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
Nov 16 '05 #8
Bradley1234 <so*****@yahoo.com> wrote:
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;
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).


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bradley1234 <so*****@yahoo.com> wrote:
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;

> 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).


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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.
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Atara | last post by:
I have a control on a userControl1 on a userControl2 on a userControl3 on a Panel on a Form and sometimes I cannot close my form. I understand it is a known bug that was already discussed, but I...
1
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to...
0
by: mawi | last post by:
Hello, Description: I create panels with some controls on a page using a new panel button. One of the controls on each panel is the "close panel" button that is supposed to close the panel it...
2
by: Michael | last post by:
Need some help trying to read values from web controls - specifically *finding* the controls (like a drop down list) - that are added dynamically added within an asp:panel control. The page...
6
by: dhnriverside | last post by:
Hi peeps, I'm trying to create some controls textboxes at runtime, based on the number of items in a IETreeView that are checked. That I can do, I've got a place holder and I can create the...
4
by: Bass Pro | last post by:
Hi, I am creating textbox, radiobuttonlist and checkboxlist dynamically depending on data from a table. It is a questionnaire. I add the control on a Panel control during the 1st load_page event....
0
by: Gman | last post by:
Hi, Objective: Draw a grid on a bitmap and set this as a panel's image. (Rather than draw the grid directly on the panel and redraw repeatedly in the paint event.) Problem: It works fine....
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.