Adding a horizontal separator in a MenuStrip
Asked Answered
G

11

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?

Groceryman answered 2/2, 2009 at 19:45 Comment(0)
C
38

I'm able to run code like this:

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

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

Callison answered 2/2, 2009 at 19:54 Comment(4)
I am using the GUI Designer and it tells me when I try to do '-' that 'Cannot add ToolStripSeparator to MenuStrip'Groceryman
That works when I add it to the MainForm.Designer.cs, thanks!Groceryman
Actually that doesn't work. It shows up in designer, but not when I actually run the program...Groceryman
I added mnuMain.Items.Insert(2, new ToolStripSeparator()); to get the desired effect.Groceryman
J
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("-"));
Jocular answered 3/12, 2010 at 10:40 Comment(4)
LOL! Why is this not flagged as answer? This is the simplest and the best way rather than typing any code.Arminius
When I try to do that: "Cannot add ToolStripSeparator to MenuStrip"Marcelina
Wow, and here I was editing designer files because I couldn't find a clean way in the designer UI to insert it. This is perfect.Recline
A simplification to this solution contextMenu1.MenuItems.Add("-");Arachne
C
38

I'm able to run code like this:

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

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

Callison answered 2/2, 2009 at 19:54 Comment(4)
I am using the GUI Designer and it tells me when I try to do '-' that 'Cannot add ToolStripSeparator to MenuStrip'Groceryman
That works when I add it to the MainForm.Designer.cs, thanks!Groceryman
Actually that doesn't work. It shows up in designer, but not when I actually run the program...Groceryman
I added mnuMain.Items.Insert(2, new ToolStripSeparator()); to get the desired effect.Groceryman
S
12

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

Saracen answered 26/11, 2011 at 3:41 Comment(0)
K
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...

Kieffer answered 24/10, 2022 at 15:41 Comment(1)
My menu is created programmatically and this approach is the only one that worked for me.Burack
K
0
  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
Kela answered 16/6, 2013 at 20:8 Comment(0)
P
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 :)

Pucker answered 24/6, 2014 at 10:24 Comment(0)
M
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.

Massarelli answered 10/1, 2016 at 14:10 Comment(0)
F
0

It' so simple,

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

Flavio answered 5/8, 2016 at 9:52 Comment(0)
P
0

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.

Premeditation answered 3/11, 2016 at 9:54 Comment(1)
There literally is a separator class.Catch
C
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).

Catch answered 21/5, 2021 at 2:21 Comment(0)
B
0

Just add "-" to the place you want in your menustrip.

enter image description here

Buna answered 17/11, 2023 at 18:49 Comment(1)
How does this add anything new compared to this much earlier answer?Oarsman

© 2022 - 2024 — McMap. All rights reserved.