在 MenuStrip 中添加水平分隔符

发布于 2024-07-13 06:14:24 字数 114 浏览 6 评论 0原文

我似乎找不到任何方法在 MenuStrip 中添加水平分隔符。 Visual Studio 抱怨无法将 ToolStropSeparator 添加到 MenuStrip。

知道我该怎么做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

雪落纷纷 2024-07-20 06:14:24

在您想要用分隔线分隔的两个字段之间的空间中,键入:

-

然后按 Enter 键(在设计器中)

如果您需要以编程方式执行此操作,您可以使用相同的技巧:

contextMenu1.MenuItems.Add(new MenuItem("-"));

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("-"));
肤浅与狂妄 2024-07-20 06:14:24

我能够运行这样的代码:

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

没有任何问题...您遇到了什么类型的错误?

I'm able to run code like this:

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

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

偏爱你一生 2024-07-20 06:14:24

您可以右键单击菜单栏,然后​​选择“插入”-> '分隔器'。
就这样。

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

人│生佛魔见 2024-07-20 06:14:24

我的一个项目遇到了问题,我完全丢失了主菜单,我必须以编程方式重新添加它,但我无法重新添加分隔符。 因此,经过一段时间的努力,我可以把一切都找回来。 这就是我再次将分隔符重新添加到菜单中所做的操作:

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 */
}

要使用 MenuStrip 或 ContextMenuStrip 执行此操作,只需将“DropDownItems”替换为“Items”。

就是这样。 我希望它有帮助...

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

嘿咻 2024-07-20 06:14:24

脚步:
1.右键单击上下文菜单条带项目-> 插入-> 分隔符

  1. a) 单击要放置水平分隔符的位置(ContextMenuStrip 的类型文本框),然后输入“-”(减号)
    b) 输入
    希望这对您有用:)

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

扭转时空 2024-07-20 06:14:24

我喜欢用 - 而不是分隔符。 假设我想制作一个水平分隔符,我会做大约 30 个分隔符来创建一条长度为 30 的水平虚线。然后我将其启用属性设置为 false,这样就无法像垂直分隔符那样单击它。 就做| 然后您的每个项目的项目,您会注意到它将创建一条与每个项目匹配的垂直线。 [注意] 由于每个项目之间的间距,垂直线会出现虚线。

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.

烦人精 2024-07-20 06:14:24

就这么简单,

右键单击上下文菜单条图标。
然后选择插入,然后选择分隔符 |

It' so simple,

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

小…红帽 2024-07-20 06:14:24

菜单条项目没有分隔符。

然而;

您可以选择要在其之间留有空格的项目,然后设置左边距或右边距值。 这也同样有效。

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.

草莓酥 2024-07-20 06:14:24
  1. 将 MenuStrip 添加到表单
  2. 单击 MenuStrip 并单击属性窗口中的项目“...”
  3. 在打开的窗口中的“选择项目并添加到下面的列表”部分中单击添加
    例如对于 3 然后选择示例 toolStripMenuItem1 并单击 DropDownItems“...”然后在新打开的窗口部分中“选择项目并添加到下面的列表”选择分隔符| 并添加它。
    好锁
  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
情绪 2024-07-20 06:14:24

由于某种原因,上述与设计师相关的答案似乎在 VS 2019 (16.8.4) 中对我不起作用。 缺少插入分隔符选项,并且在文本中使用“-”似乎不起作用。

我可以添加分隔符的唯一方法是在设计器中执行以下操作:

顶部菜单条上的人民币 -> 选择Edit DropDownItems

这将弹出一个对话框,允许您添加分隔符(以及任何其他菜单项)。

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

晨曦÷微暖 2024-07-20 06:14:24

只需将“-”添加到菜单条中所需的位置即可。

输入图片此处描述

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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文