25

I can't seem to find any way to add a horizontal separator in a MenuStrip. Visual Studio complains Cannot add ToolStropSeparator to MenuStrip.

Any idea's how I can do this?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Malfist
  • 31,179
  • 61
  • 182
  • 269

10 Answers10

68

In the space between the two fields you want separated by the divider, type:

-

then hit enter (in the designer)

If you need to do this programmatically you can use the same trick:

contextMenu1.MenuItems.Add(new MenuItem("-"));
jerre
  • 5
  • 4
Gerard
  • 691
  • 1
  • 5
  • 2
36

I'm able to run code like this:

this.menuMain.Items.Add(new ToolStripSeparator());

without any trouble... What kind of error are you getting?

GWLlosa
  • 23,995
  • 17
  • 79
  • 116
12

You can right-click the menustrip, then 'Insert' -> 'Separator'. That's all.

WhySoSerious
  • 1,930
  • 18
  • 18
1

I had an issue with one of my projects where I completely lost my main menu and I had to re-add it programatically, but I could not re-add the separators. So after trying for quite a while I could get it all back. This is what I did to re-add the separators to the menu again:

private void CreateMainMenu()
{
    // I was using a ToolStripMenuItem, so I did this...

    // Just in case, clear the items
    MainMenu.DropDownItems.Clear();

    menu.Items.Add(MainMenu);
    MainMenu.DropDownItems.AddRange(new ToolStripMenuItem[]{
            
        this.optionsMainMenu,
        this.manageLibrariesMainMenu,
        this.helpMainMenu,
        this.aboutMainMenu,
        this.checkForUpdatesMainMenu,
        this.quitMainMenu

     });
        
     // This adds the separators to a specific location
     MainMenu.DropDownItems.Insert(1, new ToolStripSeparator());
     MainMenu.DropDownItems.Insert(3, new ToolStripSeparator());

     optionsMainMenu.DropDownItems.AddRange(new ToolStripItem[]{

         this.serverUpTimeLimitToolStripMenuItem,
         this.enableOnLoadMainMenu,
         this.showInTaskBarMainMenu,
         this.alwaysOnTopMainMenu
        
     });

     optionsMainMenu.DropDownItems.Insert(1, new ToolStripSeparator());

     /* The index number after the "Insert" method corresponds to an
      * integer which will locate the separator in the item collection */
}

For doing this with a MenuStrip or ContextMenuStrip just replace "DropDownItems" with "Items".

That's it. I hope it helps...

0

steps: 1. right click on contextmenuStrip items - > Insert -> Separator

OR

  1. a) Click on (Type Text box for ContextMenuStrip) where you want to put the horizontal separator, then enter" - "(Minus sign) b) Enter hope this will find you useful :)

verena
  • 19
  • 8
0

I like to do mine with - rather then a separator. So say i wanna make a horizontal separator i would do about 30 of them to create a horizontal dotted line with a length of 30. Then i would set its enabled property to false so it can't be clicked as for a vertical one. Just do | then your item for each item and you will notice it will create a vertical line matching up with each item. [Note] The vertical line will be dotted due to the spacing between each item.

0

It' so simple,

Right click on the Context menu strip icon. then Select insert and after select Separator |

0

For some reason none of the above designer related answers seemed to work for me in VS 2019 (16.8.4). Insert Separator option is missing and using "-" in the text does not seem to work.

The only way I could add a separator was to do the following in designer:

RMB on the top menu strip -> select Edit DropDownItems

This brings up a dialogue box that allows you to add separators (as well as any other menu items).

bytedev
  • 8,252
  • 4
  • 48
  • 56
-1
  1. Add The MenuStrip to Form
  2. Click MenuStrip And Click Items "..." In Properties Window
  3. In Opened Window in Section "Select item and add to list below" Click Add
    For Example For 3 Then Select the for example toolStripMenuItem1 and Click DropDownItems "..." Then in Section to New Opened Window "Select item and add to list below" Select Separator | And Add it. Good Lock
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Mojtaba
  • 31
  • 2
-1

There are no separators for menu strip items.

However;

You can select the item you want a space between, and set the left or right margin value. This works just as well.

dave88
  • 312
  • 5
  • 14