如何在ContextMenuStrip中设置默认菜单项

本文关键字:设置 默认 菜单项 ContextMenuStrip | 更新日期: 2023-09-27 18:12:18

在我的应用程序中,我使用右键单击对象时弹出菜单项。我使用如下代码动态地构建这个菜单:

ContextMenuStrip menu = new ContextMenuStrip();
menu.Items.Add(new ToolStripMenuItem("Item1", aNiceImage, someFunction));
menu.Items.Add(new ToolStripMenuItem("Item2", alsoNiceImage, someOtherFunction));

现在我想将其中一个菜单项设置为粗体(正如Windows用户体验指南所建议的那样),以指示与双击对象对应的操作。

我该怎么做?

如何在ContextMenuStrip中设置默认菜单项

使用item.Font = new Font(item.Font, item.Font.Style | FontStyle.Bold)对当前字体进行加粗效果

您还可以自动选择默认项,如下所示:

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) 
{
  contextMenuStrip1.Items[3].Select();
}

使用Font属性指定所需的FontStyle:

字体
myToolStripMenuItem.Font = new Font(
    FontFamily.GenericSansSerif,
    12.0F, FontStyle.Bold);

明显改变输入以获得期望的输出,FontStyle.Bold是这里的重要部分。