I am trying to dynamically build up a menustrip at runtime from data in a string such as ...
Item1~Item2~Item3~Item4
where each item needs to be a subitem of the previous one in the menu.
I can convert the string to an array, loop through the items and get the data required.
However, I want to be able to efficiently add the sub items to their parent without continually looping through all the item names in the entire menu to see if and where the item already exists.
I know the following code will work but it has to be wildly inefficient when dealing with alot of entries as it looks through the entire menu system each time.
Expand|Select|Wrap|Line Numbers
- For j = 0 To myArray.Count - 1
- Dim thisid = myArray(j)
- Dim itemfound = 0
- Dim additem As New ToolStripMenuItem() With {.Text = itemid, .Name = j}
- For Each menuitem As ToolStripDropDownItem In mainMenu.DropDownItems
- If menuitem.Name = j Then
- menuitem.DropDownItems.Add(additem)
- itemfound = 1
- Exit For
- End If
- Next
- ' if not found then add it ... etc
What I can't (yet again) work out is how to change the ForEach line to target the check for existing items in the previous item only. That is...
For Each menuitem As ToolStripDropDownItem In <PREVITEM>.DropDownItems
where <PREVITEM> is Item2 when looking to add Item3.
Does this make any sense at all? :)
Thanks