115

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?

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
Adam Pierce
  • 33,531
  • 22
  • 69
  • 89

8 Answers8

244

I believe it's just a dash:

ContextMenu.MenuItems.Add("-");
SqlRyan
  • 33,116
  • 33
  • 114
  • 199
  • 1
    This is one of many poorly documented items in Windows. I needed to do this a few months ago. I remembered that I could do it in Win32, but couldn't remember the syntax. I ended up pulling up some old VC++ 6 files to find it. By the way, I still occasionally refer to "The Petzold Book" for some things. Wow, I'm feeling old... – Brad Bruce Aug 29 '09 at 01:27
  • 32
    Surely this is more readable `ContextMenu.Items.Add(new ToolStripSeparator());` see [Gabriel's answer](http://stackoverflow.com/a/5880762/15639) – MarkJ Mar 18 '12 at 12:28
  • 9
    @MarkJ, The ContextMenu no longer has an Items property. ToolStripSeperator() is also not a valid argument for ContextMenu.Items.Add(). – Cypher Jun 25 '12 at 06:04
  • 4
    Note that you can do this visually by entering the hyphen in the "Type here" entry field of the menu designer in the Visual Studio Windows Forms designer. The separator will appear immediately in the designer. – Buggieboy Jan 16 '13 at 18:43
  • @SqlRyan Can you pls look into this question, I suppose you can help me with this. – Ananthu K Kumar Mar 18 '21 at 08:57
  • Looks like this might be borken in .NET 6.0 - I am now seeing a dash instead of a separator. – David Airapetyan Jan 25 '23 at 00:45
61

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());
Gabriel
  • 635
  • 5
  • 2
11

In WPF:

ContextMenu.MenuItems.Add(new Separator());
al2suarez
  • 129
  • 1
  • 3
7

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.

Aziz
  • 20,065
  • 8
  • 63
  • 69
3

Set the text property to a hyphen.

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
3

Horizontal separators are cool, but what if you want a vertical separator instead?

Well, worry ye not - you can have one!

Set BarBreak property to true on the MenuItem which should be the first one after the seperator:

var item = new MenuItem(text: "Settings", onClick: SomeFunction) { BarBreak = true };

enter image description here

To add the item to a MenuItems collection: yourContextMenu.MenuItems.Add(item).

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
1

Perhaps in later versions of Visual Studio they made this simpler. I'm using VS 2012. You can add a separator via the forms designer. 1) Select/Create a MenuStrip. 2) On "Type Here", right mouse. 3) Select "Insert". 4) Select "Separator". 5) Drag the new separator to the text you want it to be above. Done.

JimMoore
  • 31
  • 2
0

ContextMenu has a constructor which receives an array of MenuItem objects. Needless to say, you can't add a string to that array. You can however get a seperator by adding a new MenuItem("-"):

    var contextMenu = new ContextMenu(new[]
    {
        timerMenuItem,
        keypressMenuItem,
        new MenuItem("-"), // Seperator
        new MenuItem(text: "Exit", onClick: (sender, args) => Application.Exit())
    });
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108