Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a separator to a WinForms ContextMenu?

Inside my control, I have:

ContextMenu = new ContextMenu();
ContextMenu.MenuItems.Add(new MenuItem("&Add Item", onAddSpeaker));
ContextMenu.MenuItems.Add(new MenuItem("&Edit Item", onEditSpeaker));
ContextMenu.MenuItems.Add(new MenuItem("&Delete Item", onDeleteSpeaker));
ContextMenu.MenuItems.Add( ??? );
ContextMenu.MenuItems.Add(new MenuItem("Cancel"));

How to add a separation line to this ContextMenu?

like image 289
Adam Pierce Avatar asked Aug 28 '09 23:08

Adam Pierce


4 Answers

I believe it's just a dash:

ContextMenu.MenuItems.Add("-");
like image 70
SqlRyan Avatar answered Nov 20 '22 12:11

SqlRyan


This works just as well as the dash, and i suspect the Winforms will translate the dash to a ToolStripSeparator. I for one think this solution is more obvious for anyone who has to maintain the code.

yourContextMenu.Items.Add(new ToolStripSeparator());
like image 26
Gabriel Avatar answered Nov 20 '22 12:11

Gabriel


In WPF:

ContextMenu.MenuItems.Add(new Separator());
like image 11
al2suarez Avatar answered Nov 20 '22 10:11

al2suarez


If you are using the Designer, place a single hyphen "-" as text the same way you would name your menu items. After hitting enter, the separator will be created.

like image 7
Aziz Avatar answered Nov 20 '22 10:11

Aziz