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.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With