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 9 15300
Hello,
"Merlin" <je**@jg-tech.co.uk> schrieb: 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
--
MVP · VB Classic, VB .NET http://www.mvps.org/dotnet
"Merlin" <je**@jg-tech.co.uk> schrieb 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
--
Armin
> 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
"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:L7*******************@twister.tampabay.rr.com ... 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
> > 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!
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Charles Law |
last post by:
I have a form on which user controls are placed at runtime. When a control
is added to the form a handler is added for an event that a high-level
object raises, which must be handled by the new...
|
by: Daniel Friend |
last post by:
Hello,
I have added a custom usercontrol programically and all works fine... I
would like to change some custom properties that I have set in that
usercontrol.
Any help would be greatly...
|
by: Agnes |
last post by:
In myDatagrid, there is 3 columns, account code, debit,credit.
If the user input some particular a account code in column1 , i need to
based on the contect of column1, to disable...
|
by: Jim |
last post by:
I have a datagrid with a DataAdapter as the DataSource. The user fills in
their data for 3 columns and I want to programically add a value to the 4th
(invisible) column (employee number). That way...
|
by: dtarczynski |
last post by:
Hello.
Im trying to add new EventHandler to DropDownList programically. Im
doing something like this:
DropDownList ddlCate = new DropDownList();
ddlCate.ID = String.Format("ddlCategory{0}",...
|
by: superjacent |
last post by:
Hope someone can help.
I have a saved parent form containing an unbound sub-form. I set the
SourceObject (form) after the Parent Form opens/loads.
The sub-form (datasheet view) basically...
|
by: abduzalam |
last post by:
Hi Members,
I am in a serious trouble,My problem is i want to develop a software as my academic project, it is network back up database, that is a remote machine hold sql databses, and when...
|
by: =?Utf-8?B?U3RlcGhhbmllIERvaGVydHk=?= |
last post by:
Can anyone tell me how to programmatically remove a control from a tab page
from VB .Net? The control was added using:
Me.TabPage1.Controls.Add(pb)
but I don't know what its index number is to...
|
by: =?Utf-8?B?R3JlZw==?= |
last post by:
I have the following code the dynamically adds a specific number of controls.
for x as integer = 1 to 10
Dim btn as Windows.Forms.Button = New Windows.Forms.Button
btn.Name = "btn" & x
btn.Text...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |