Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting error when i am trying to remove child menu items.
Error : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
see my code
-------------
Dim mnuItems As New MenuItem()
Dim menuItems As MenuItemCollection = NavMenu.Items
Dim MasterItem As New MenuItem()
For Each menuItem As MenuItem In menuItems
If menuItem.Value = "First" Then
MasterItem = menuItem
End If
If menuItem.Value = "Second" Then
mnuItems.ChildItems.Remove(mnuItems.ChildItems(0)) ''Getting Error here.
End If
Next
menuItems.Remove(MasterItem)
Error :
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Posted
Comments
merridus 7-Feb-12 4:13am    
Does mnuItems actually have any children to remove?

It looks like you are trying to remove the first child when there are none which would produce the out of range exception. Unless you are adding children to it somewhere else?

1 solution

Try
if (mnuItems.ChildItems.Count >0) Then mnuItems.ChildItems.Remove(mnuItems.ChildItems(0)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900