Adding ToolStripMenuItem to ContextMenuStrip at specific index
Asked Answered
E

2

6

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.

Eruct answered 31/5, 2011 at 11:41 Comment(0)
S
10

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);
Sapsago answered 31/5, 2011 at 12:0 Comment(0)
U
1

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
Unmixed answered 31/5, 2011 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.