By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

Build Menustrip at runtime

100+
P: 116
Hi,

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
  1. For j = 0 To myArray.Count - 1
  2. Dim thisid = myArray(j)
  3. Dim itemfound = 0
  4. Dim additem As New ToolStripMenuItem() With {.Text = itemid, .Name = j}
  5. For Each menuitem As ToolStripDropDownItem In mainMenu.DropDownItems
  6. If menuitem.Name = j Then
  7. menuitem.DropDownItems.Add(additem)
  8. itemfound = 1
  9. Exit For
  10. End If
  11. Next
  12. ' if not found then add it ... etc
  13.  
I would have thought a better way is to only look at the items in Item1 when adding Item2 (if no match then add it), then only look at items in Item2 for Item3 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
Mar 15 '13 #1

✓ answered by robertybob

Ok I have isolated this problem to being able to dynamically determine the parent item.

So for the first item added I can use

Expand|Select|Wrap|Line Numbers
  1. For Each subitem As ToolStripDropDownItem In DirectCast(mainMenu.Items(Item1), ToolStripDropDownItem).DropDownItems
  2.  
For the next one I need to alter 'mainMenu.Items' to 'Item1.DropDownItems' and so on...

Not sure how best to do that dynamically.

Share this Question
Share on Google+
2 Replies


100+
P: 116
Actually, typing this out might have helped :)

Am currently trying DirectCast(mainMenu.Items(previtem), ToolStripDropDownItem).DropDownItems to see it thats it.

Will let you know it solved
Mar 15 '13 #2

100+
P: 116
Ok I have isolated this problem to being able to dynamically determine the parent item.

So for the first item added I can use

Expand|Select|Wrap|Line Numbers
  1. For Each subitem As ToolStripDropDownItem In DirectCast(mainMenu.Items(Item1), ToolStripDropDownItem).DropDownItems
  2.  
For the next one I need to alter 'mainMenu.Items' to 'Item1.DropDownItems' and so on...

Not sure how best to do that dynamically.
Mar 15 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.