How to add a separator to a WinForms ContextMenu?

C#WinformsContextmenuSeparator

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

I believe it's just a dash:

ContextMenu.MenuItems.Add("-");

Solution 2 - C#

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());

Solution 3 - C#

In WPF:

ContextMenu.MenuItems.Add(new Separator());

Solution 4 - C#

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.

Solution 5 - C#

Set the text property to a hyphen.

Solution 6 - C#

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).

Solution 7 - C#

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.

Solution 8 - C#

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())
	});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam PierceView Question on Stackoverflow
Solution 1 - C#SqlRyanView Answer on Stackoverflow
Solution 2 - C#GabrielView Answer on Stackoverflow
Solution 3 - C#al2suarezView Answer on Stackoverflow
Solution 4 - C#AzizView Answer on Stackoverflow
Solution 5 - C#shahkalpeshView Answer on Stackoverflow
Solution 6 - C#Stephen KennedyView Answer on Stackoverflow
Solution 7 - C#JimMooreView Answer on Stackoverflow
Solution 8 - C#Stephen KennedyView Answer on Stackoverflow