如何向 WinForms ContextMenu 添加分隔符?
在我的控件内,我有:
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"));
如何向此 ContextMenu 添加分隔线?
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"));
How to add a separation line to this ContextMenu?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我相信这只是一个破折号:
I believe it's just a dash:
这与破折号一样有效,我怀疑 Winforms 会将破折号转换为 ToolStripSeparator。我认为这个解决方案对于任何必须维护代码的人来说都更明显。
This works just as well as the dash, and i suspect the Winforms will translate the dash to a ToolStripSeparator. I for one think this solution is more obvious for anyone who has to maintain the code.
在 WPF 中:
In WPF:
如果您使用设计器,请放置一个连字符“-”作为文本,就像命名菜单项一样。按 Enter 键后,将创建分隔符。
If you are using the Designer, place a single hyphen "-" as text the same way you would name your menu items. After hitting enter, the separator will be created.
将文本属性设置为连字符。
Set the text property to a hyphen.
水平分隔符很酷,但是如果您想要垂直分隔符怎么办?
好吧,不用担心——你可以拥有一个!
设置
将
属性设置为MenuItem
上的 BarBreaktrue
,该属性应该是分隔符之后的第一个:添加项目到
MenuItems
集合:yourContextMenu.MenuItems.Add(item)
。Horizontal separators are cool, but what if you want a vertical separator instead?
Well, worry ye not - you can have one!
Set
BarBreak
property totrue
on theMenuItem
which should be the first one after the seperator:To add the item to a
MenuItems
collection:yourContextMenu.MenuItems.Add(item)
.也许在 Visual Studio 的更高版本中,他们使这变得更简单。我使用的是 VS 2012。您可以通过表单设计器添加分隔符。
1) 选择/创建一个菜单条。
2) 在“在此键入”上,右键单击。
3) 选择“插入”。
4) 选择“分隔符”。
5) 将新分隔符拖至您希望其位于上方的文本。
完毕。
Perhaps in later versions of Visual Studio they made this simpler. I'm using VS 2012. You can add a separator via the forms designer.
1) Select/Create a MenuStrip.
2) On "Type Here", right mouse.
3) Select "Insert".
4) Select "Separator".
5) Drag the new separator to the text you want it to be above.
Done.
ContextMenu
具有 一个构造函数,它接收MenuItem
对象数组。不用说,您不能将字符串添加到该数组中。不过,您可以通过添加new MenuItem("-")
来获取分隔符:ContextMenu
has a constructor which receives an array ofMenuItem
objects. Needless to say, you can't add a string to that array. You can however get a seperator by adding anew MenuItem("-")
: