Search code examples
c#.netwinformscontextmenustrip

How to rearrange items from a contextmenustrip?


How do I rearrange items of a ContextMenuStrip? For example if I create a windows forms app with a button and add a ContextMenuStrip to the form and assign it on the button click event and add items in the sequence

run process1
run process2
run process3

and then after some day I decide to add another item to the ContextMenuStrip say "run process4" and I want the sequence to be something like

run process1
run process4
run process2
run process3

How can I do that (apart from renaming every single item and interchanging the codes on each of the click event)?


Solution

  • In addition to Add items you can also Insert item at specified index:

    this.contextMenuStrip1.Items.Add("Item1");
    this.contextMenuStrip1.Items.Add("Item2");
    this.contextMenuStrip1.Items.Add("Item3");
    this.contextMenuStrip1.Items.Insert(1, new ToolStripMenuItem("Item4"));