Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding ToolStripMenuItem to ContextMenuStrip at specific index

Is it possible to add ToolStripMenuItems to ContextMenuStrip at a specific index? I have a list of items and I want to add them to a ContextMenuStrip and I want to know if its possible to add the items to the ContextMenu at specific index.

This is my list:

Item1
Item2 
Item3 
Item4 

I want to add them to the ContextMenu so they appear like this in the menu:

Item2
Item3
Item1
Item4

Is it possible to do that?

All help is very appreciated.

like image 936
Erik Avatar asked May 31 '11 11:05

Erik


2 Answers

If you are using the designer to add you can just move the items up and down using the arrows in the designer view.

If you are using code to add you can just using the Insert method:

contextMenuStrip1.Items.Insert(1, item);
like image 113
Oskar Kjellin Avatar answered Sep 30 '22 20:09

Oskar Kjellin


You cannot assign items directly to the collection, such as contextMenuStrip1.Items(2) = "Item2", but you can accomplish the same thing by adding the items in order, or by using the insert and delete methods.

Dim item As New ToolStripMenuItem

item.Text = "item B"
contextMenuStrip1.Items.Insert(1, item) ' inserts "item B" before the second menu item.
contextMenuStrip1.Items.Delete(contextMenuStrip1.Items(2)) ' deletes the third menu item
like image 24
xpda Avatar answered Sep 30 '22 21:09

xpda