|
-
Oct 6th, 2011, 07:52 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Removing controls
Hi,
I'm using this code to remove controls from a panel...
vb.net Code:
For Each c As Control In Me.pnlDevices.Controls Me.pnlDevices.Controls.Remove(c) Next
...but somehow it doesn't remove all the controls at once, I have to run the code 3 times to remove them all
(there is about 20 to 30 of them, mainly labels)...
I don't know if it matters, but the controls where added dynamically at startup...
Last edited by Arve K.; Oct 6th, 2011 at 07:53 PM.
Reason: typo + added some info
-
Oct 6th, 2011, 07:54 PM
#2
Re: Removing controls
I'm surprised that that code actually runs at all. I would have expected it to throw an exception. NEVER enumerate a list using a For each loop and then modify the list inside the loop. If you want to clear a list completely then, if it has one, call its Clear method. Otherwise, use a Do or While loop or else use a For loop and go from end to beginning rather than beginning to end.
-
Oct 6th, 2011, 07:56 PM
#3
Re: Removing controls
try this instead:
vb Code:
For x As Integer = Me.pnlDevices.Controls.count - 1 To 0 Step -1
Me.pnlDevices.Controls.RemoveAt(x)
Next
your code modifies Me.pnlDevices.Controls in a for each loop causing the for each to fail.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 6th, 2011, 08:01 PM
#4
Thread Starter
Fanatic Member
Re: Removing controls
Oh, I never thought of it that way... No wonder it didn't work 
Thank you both for you help, problem solved!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|