|
-
Mar 22nd, 2008, 01:30 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] me.controls.remove() problem (HELP!!! :-( )
Hey guys,
I am setting up several controls on a form at run time using
Code:
dim lblMessage as label = new label
and then after setting up its location and text, etc and adding it to the forms controls using
Code:
me.controls.add(lblMessage)
Now when the form is closed i call a function which disposes of all the controls on the form.
In this function i am using the code
Code:
For Each ctrl as Control in Me.Controls
Me.Controls.Remove(ctrl)
ctrl.dispose()
Next
but for some reason to which i am not aware it is only removing and disposing half of all the controls on the form. If i get rid of the code and message out the name of all the controls on the form then it lists all of them but when trying to delete them, it appears to stop half way through.
I have also output a message after the code to remove the controls stating how many controls are left on the form (incase it somehow wasn't actually removing them and just hiding the instead) and the message that is displayed shows that there are indeed several controls remaining. I have racked my brain as to the reasons it is doing this and have gained nothing except for a headache, so can anyone else shed some light on the situation and possible tell me where things are going wrong and what i can possibly do to get this to work properly?
Thank you in advance for any help
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Mar 22nd, 2008, 02:00 PM
#2
Fanatic Member
Re: [2005] me.controls.remove() problem (HELP!!! :-( )
isn't the prblem comming from the fact thatyou are modifying the cllection that your are actually looping in? When you remove a control, the pointer goes to the next position in the collection. On the other side the collection is modified and the supposely next becomes previous.
Basically doing what your are actually wrote will mean that you want to remove at position 0 all the time.
By disposing of the form, doesn't it dispose of all its controls automatically?
-
Mar 22nd, 2008, 04:30 PM
#3
Thread Starter
Fanatic Member
Re: [2005] me.controls.remove() problem (HELP!!! :-( )
Hi, thanx, I have used this method before with it working fine, i dont want to dispose of the form because it is the main form of the program.
I will try creating a new form and then disposing of the old form, but if anybody else has a different way that would prevent me from doing this then i appreciate the input.
Thanks again.
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Mar 22nd, 2008, 07:51 PM
#4
Re: [2005] me.controls.remove() problem (HELP!!! :-( )
vb.net Code:
For Each ctl As Control In Me.Controls ctl.Dispose() Next ctl Me.Controls.Clear()
-
Mar 22nd, 2008, 08:07 PM
#5
Re: [2005] me.controls.remove() problem (HELP!!! :-( )
 Originally Posted by talkro
isn't the prblem comming from the fact thatyou are modifying the cllection that your are actually looping in? When you remove a control, the pointer goes to the next position in the collection. On the other side the collection is modified and the supposely next becomes previous.
Basically doing what your are actually wrote will mean that you want to remove at position 0 all the time.
By disposing of the form, doesn't it dispose of all its controls automatically?
The problem is exactly that and it is also documented in MSDN:
When Remove deletes an element from a collection, it decrements the collection's Count Property (Collection Object) by one. It also decrements the Index value of every element that formerly followed the deleted element in the collection.
You can see below a demonstration of how it behaves and why the problem would occur by looking at your loop in more deatil This is a collection with siz elements names 1 to 6.
Code:
Pass 1
--> 1
2
3
4
5
6
Remove 1
--> 2
3
4
5
6
Advance Pointer
2
--> 3
4
5
6
Pass 2
2
--> 3
4
5
6
Remove 3
2
--> 4
5
6
Advance Pointer
4
--> 5
6
Pass 2
4
--> 5
6
Remove 5
2
--> 6
Advance Pointer
/// END OF COLLECTION: EXIT
-
Mar 25th, 2008, 09:10 AM
#6
Thread Starter
Fanatic Member
Re: [2005] me.controls.remove() problem (HELP!!! :-( )
Thanks for all your help. It makes sense the way it works, it just confused me because i had used this method before and it appeared to work fine.
But anyways thanks again.
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
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
|