如何向 WinForms ContextMenu 添加分隔符?

发布于 2024-08-03 11:00:01 字数 417 浏览 2 评论 0原文

在我的控件内,我有:

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 技术交流群。

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

发布评论

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

评论(8

甜警司 2024-08-10 11:00:01

我相信这只是一个破折号:

ContextMenu.MenuItems.Add("-");

I believe it's just a dash:

ContextMenu.MenuItems.Add("-");
阳光下慵懒的猫 2024-08-10 11:00:01

这与破折号一样有效,我怀疑 Winforms 会将破折号转换为 ToolStripSeparator。我认为这个解决方案对于任何必须维护代码的人来说都更明显。

yourContextMenu.Items.Add(new 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.

yourContextMenu.Items.Add(new ToolStripSeparator());
天涯沦落人 2024-08-10 11:00:01

在 WPF 中:

ContextMenu.MenuItems.Add(new Separator());

In WPF:

ContextMenu.MenuItems.Add(new Separator());
悍妇囚夫 2024-08-10 11:00:01

如果您使用设计器,请放置一个连字符“-”作为文本,就像命名菜单项一样。按 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.

廻憶裏菂餘溫 2024-08-10 11:00:01

将文本属性设置为连字符。

Set the text property to a hyphen.

临风闻羌笛 2024-08-10 11:00:01

水平分隔符很酷,但是如果您想要垂直分隔符怎么办?

好吧,不用担心——你可以拥有一个!

设置 MenuItem 上的 BarBreak 属性设置为 true,该属性应该是分隔符之后的第一个:

var item = new MenuItem(text: "Settings", onClick: SomeFunction) { BarBreak = true };

在此处输入图像描述

添加项目到 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 to true on the MenuItem which should be the first one after the seperator:

var item = new MenuItem(text: "Settings", onClick: SomeFunction) { BarBreak = true };

enter image description here

To add the item to a MenuItems collection: yourContextMenu.MenuItems.Add(item).

爱你是孤单的心事 2024-08-10 11:00:01

也许在 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.

花间憩 2024-08-10 11:00:01

ContextMenu 具有 一个构造函数,它接收 MenuItem 对象数组。不用说,您不能将字符串添加到该数组中。不过,您可以通过添加 new MenuItem("-") 来获取分隔符:

    var contextMenu = new ContextMenu(new[]
    {
        timerMenuItem,
        keypressMenuItem,
        new MenuItem("-"), // Seperator
        new MenuItem(text: "Exit", onClick: (sender, args) => Application.Exit())
    });

ContextMenu has a constructor which 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("-"):

    var contextMenu = new ContextMenu(new[]
    {
        timerMenuItem,
        keypressMenuItem,
        new MenuItem("-"), // Seperator
        new MenuItem(text: "Exit", onClick: (sender, args) => Application.Exit())
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文