我似乎找不到在MenuStrip中添加水平分隔符的方法。Visual Studio会报错“无法将ToolStropSeparator添加到MenuStrip”。
你有什么想法吗?
-
然后按回车键(在设计器中)
如果您需要以编程方式执行此操作,可以使用同样的技巧:
contextMenu1.MenuItems.Add(new MenuItem("-"));
我可以运行这样的代码:
this.menuMain.Items.Add(new ToolStripSeparator());
没有遇到任何问题... 你遇到了什么样的错误?
您可以右键单击菜单栏,然后选择“插入” -> “分隔线”。就是这样。
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”即可。
就是这样。希望能对您有所帮助...
菜单栏项没有分隔符。
然而;
您可以选择要在其间添加空格的项目,并设置左侧或右侧边距值。这同样有效。
我喜欢用 - 来代替分隔符。比如说,如果我想要制作一个水平分隔线,我会输入大约30个 - 来创建长度为30的水平虚线。然后,我会将其启用属性设置为 false,这样它就不能被点击,就像垂直分隔线一样。只需在每个项目后面加上 |,你会发现它会创建一个与每个项目相匹配的垂直线。[注意]由于每个项目之间的间距,垂直线将是虚线。
或者
a) 单击 ContextMenuStrip 的 Type 文本框,将光标放在您想要放置水平分隔符的位置,然后输入“-”(减号) b) 输入 希望这对您有用 :)
这很简单,
右键单击上下文菜单栏图标。 然后选择插入,之后选择分隔符 |
由于某些原因,我在VS 2019(16.8.4)中尝试了以上与设计有关的答案,但似乎都不起作用。插入分隔符选项不见了,而在文本中使用“-”也似乎不起作用。
我唯一能够添加分隔符的方法是在设计器中执行以下操作:
右键单击顶部菜单栏 -> 选择编辑DropDownItems
这会弹出一个对话框,允许您添加分隔符(以及任何其他菜单项)。
contextMenu1.MenuItems.Add("-");
。 - user2728841