Adding ToolStripMenuItem to ContextMenuStrip at a specific index

Can I add ToolStripMenuItems to ContextMenuStrip at a specific index? I have a list of elements and I want to add them to ContextMenuStrip, and I want to know whether it is possible to add elements to ContextMenu at a specific index.

This is my list:

Item1 Item2 Item3 Item4 

I want to add them to ContextMenu so that they look like this:

 Item2 Item3 Item1 Item4 

Can this be done?

All help is much appreciated.

+6
source share
2 answers

If you use the constructor to add, you can simply move the elements up and down using the arrows in the constructor view.

If you use the code to add, you can simply use the Insert method:

 contextMenuStrip1.Items.Insert(1, item); 
+8
source

You cannot assign items directly to a collection, for example contextMenuStrip1.Items(2) = "Item2" , but you can do the same thing by adding items in order or 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 
+1
source

Source: https://habr.com/ru/post/889440/


All Articles