C# 如何向 WinForms ContextMenu 添加分隔符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1349856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a separator to a WinForms ContextMenu?
提问by Adam Pierce
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?
如何向此 ContextMenu 添加分隔线?
采纳答案by SqlRyan
I believe it's just a dash:
我相信这只是一个破折号:
ContextMenu.MenuItems.Add("-");
回答by Aziz
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.
如果您使用的是 Designer,请以与命名菜单项相同的方式放置一个连字符“-”作为文本。按回车后,将创建分隔符。
回答by shahkalpesh
Set the text property to a hyphen.
将 text 属性设置为连字符。
回答by al2suarez
In WPF:
在 WPF 中:
ContextMenu.MenuItems.Add(new Separator());
回答by Gabriel
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.
这与破折号一样有效,我怀疑 Winforms 会将破折号转换为 ToolStripSeparator。我认为这个解决方案对于任何必须维护代码的人来说更明显。
yourContextMenu.Items.Add(new ToolStripSeparator());
回答by JimMoore
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.
也许在更高版本的 Visual Studio 中,他们使这更简单。我正在使用 VS 2012。您可以通过表单设计器添加分隔符。1) 选择/创建一个 MenuStrip。2) 在“Type Here”上,鼠标右键。3) 选择“插入”。4) 选择“分隔符”。5) 将新的分隔符拖到您希望它位于上方的文本上。完毕。
回答by Stephen Kennedy
ContextMenu
has a constructorwhich receives an array of MenuItem
objects. Needless to say, you can't add a string to that array. You can however get a seperator by adding a new MenuItem("-")
:
ContextMenu
有一个构造函数,它接收一个MenuItem
对象数组。不用说,您不能向该数组添加字符串。但是,您可以通过添加一个来获得一个分隔符new MenuItem("-")
:
var contextMenu = new ContextMenu(new[]
{
timerMenuItem,
keypressMenuItem,
new MenuItem("-"), // Seperator
new MenuItem(text: "Exit", onClick: (sender, args) => Application.Exit())
});
回答by Stephen Kennedy
Horizontal separators are cool, but what if you want a vertical separator instead?
水平分隔符很酷,但如果你想要一个垂直分隔符呢?
Well, worry ye not - you can have one!
好吧,不用担心 - 你可以拥有一个!
Set BarBreak
property to true
on the MenuItem
which should be the first one after the seperator:
将BarBreak
属性设置为true
onMenuItem
应该是分隔符之后的第一个:
var item = new MenuItem(text: "Settings", onClick: SomeFunction) { BarBreak = true };
To add the item to a MenuItems
collection: yourContextMenu.MenuItems.Add(item)
.
要将项目添加到MenuItems
集合:yourContextMenu.MenuItems.Add(item)
。