How to add a separator to a WinForms menu in C #?

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

What do I need to do to make the menu separator?

+78
c # winforms
Aug 28 '09 at 23:24
source share
6 answers

I believe this is just a dash:

 ContextMenu.MenuItems.Add("-"); 
+176
Aug 28 '09 at 23:26
source share

This works as well as the dash, and I suspect that Winforms will translate the dash into ToolStripSeparator. I believe this solution is more obvious to anyone who needs to maintain code.

 yourContextMenu.Items.Add(new ToolStripSeparator()); 
+34
May 4 '11 at 8:39
source share

In WPF:

 ContextMenu.MenuItems.Add(new Separator()); 
+11
Mar 17
source share

If you use the constructor, put one hyphen "-" in the form of text in the same way as the names of menu items. After pressing enter, a separator will be created.

+7
Aug 28 '09 at 23:27
source share

Set the text property for the hyphen.

+3
Aug 28 '09 at 23:28
source share

Perhaps in later versions of Visual Studio they made it easier. I am using VS 2012. You can add a separator through the form designer. 1) Select / Create MenuStrip. 2) In the Type Here field, right-click. 3) Select "Paste." 4) Select "Separator". 5) Drag the new separator into the text you want it to be above. Done.

0
Nov 14 '14 at 19:37
source share



All Articles