Programically remove controls

  • Thread starter Thread starter Merlin
  • Start date Start date
M

Merlin

Hi,

My code below doesn't work does anyone have any pointers? All my controls
are programically added.

Dim i As Int16
For i = 0 To Me.Controls.Count - 1
If Me.Controls(i).Name <> "TheOneIWantToKeep" Then
Me.Controls.RemoveAt(i)
End If
Next

As the loop moves through the index I get an Index Is Out Of Range Error mid
way through.

Many thanks
 
Hello,

Merlin said:
My code below doesn't work does anyone have any
pointers? All my controls are programically added.

Dim i As Int16
For i = 0 To Me.Controls.Count - 1
If Me.Controls(i).Name <> "TheOneIWantToKeep" Then
Me.Controls.RemoveAt(i)
End If
Next

As the loop moves through the index I get an Index Is
Out Of Range Error mid way through.

The problem is that after removing a control from the Controls collection
there are fewer controls in this collection.

HTH,
Herfried K. Wagner
 
Merlin said:
Hi,

My code below doesn't work does anyone have any pointers? All my
controls are programically added.

Dim i As Int16
For i = 0 To Me.Controls.Count - 1
If Me.Controls(i).Name <> "TheOneIWantToKeep" Then
Me.Controls.RemoveAt(i)
End If
Next

As the loop moves through the index I get an Index Is Out Of Range
Error mid way through.

For i = Me.Controls.Count - 1 To 0 step -1
 
Dim i As Int16
For i = 0 To Me.Controls.Count - 1
If Me.Controls(i).Name <> "TheOneIWantToKeep" Then
Me.Controls.RemoveAt(i)
End If
Next

As the loop moves through the index I get an Index Is Out Of Range Error mid
way through.

Because the controls are being deleted as you traverse the collection, the
index sooner or later becomes invalid. The safest way is to use a For...Each
Loop:

Dim ThisCtrl as Control

For Each ThisCtrl in Me.Controls
Me.Controls.Remove( ThisCtrl )
Next

HTH

~
Jeremy

"Knowing others is intelligence;
knowing yourself is true wisdom.
Mastering others is strength;
mastering yourself is true power."
-Lao Tzu
 
For Each ThisCtrl in Me.Controls
if ThisCtrl.Name <> "TheOneIWantToKeep" Then Me.Controls.Remove(
ThisCtrl )
Next

But I find the one from Armin nice too.
 
Jeremy,
Because the controls are being deleted as you traverse the collection, the
index sooner or later becomes invalid. The safest way is to use a For...Each
Loop:
For each & removing items from a collection are normally incompatible!
Control.Controls happens to be an exception. Well partial exception, it
doesn't work correctly. If you start relying on this pattern, it will bite
you later! (Try it on a HashTable or ArrayList).

Try a for each loop on an inherited form, a number of controls are not
deleted! Some from the base, some from the derived form!

Try the follow, you will get an Invalid Operation Exception "Collection was
modified; enumeration operation may not execute".

Dim list As New ArrayList
list.Add("a")
list.Add("b")
list.Add("c")
Dim item as Object
For Each item In list
list.Remove(item)
Next

I would recommend Armin's method or a Do While loop, neither will receive
the above exception, and they both remove all the controls!

Do While Me.Controls.Count <> 0
me.controls.RemoveAt(0)
Loop

Of course Armin's may be more efficient as he is removing from the end of
the collection, while above I am removing from the beginning.

You could use:
me.controls.RemoveAt(me.controls.count -1)

to remove from the end.

Of course we all missed the sure fire way to remove all the controls.

Me.Controls.Clear()

Hope this helps
Jay
 
Because the controls are being deleted as you traverse the collection,
the
For each & removing items from a collection are normally incompatible!
Control.Controls happens to be an exception. Well partial exception, it
doesn't work correctly. If you start relying on this pattern, it will bite
you later! (Try it on a HashTable or ArrayList).

Try a for each loop on an inherited form, a number of controls are not
deleted! Some from the base, some from the derived form!

I _NEVER_ use visual inheritance (soooo buggy), I leave that up to the Beta
testers.

=)

Sorry about the oversight.

Of course we all missed the sure fire way to remove all the controls.
Me.Controls.Clear()

Nice (duh!).

~
Jeremy
 
Jay,
I was writing "I was looking at it and thought of methode like you, then
came the methode from Armin and I did think Yeah that's it in this case"
The problem is that one control has to stay and you don't know which.
Now I am in doubt, but have a while no time.
I am curious if the sollution is there when I come back, otherwise I go look
for it.
Cor
 
Jay, Armin, Jeremy, Merlin,
I did test all the methodes
I did it with a button on which event the procedure started.
The only one which works fine is the one from Armin.
The methode from Jay does absolute not work in this situation, but
because of his second answer he did know that of course already.
The methode from Jeremy throws an exeption
I did not test the second methode from Jay, I did only think about the
Treeview from Woody "Jjust remove it and add it, He kills you Jay :-)

Conclusion in my little test situation did only the methode from Armin works
fine.

Cor
 
Back
Top