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

时间:2011-10-24 13:06:16

标签: c# winforms contextmenu

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

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

现在,我想以粗体显示其中一个菜单项(由Windows用户体验指南推荐),以指示双击对象时哪个操作对应。

我该怎么做?

2 个答案:

答案 0 :(得分:20)

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

您也可以按如下方式自动选择默认项目:

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

答案 1 :(得分:3)

使用Font属性指定具有所需FontStyle的字体:

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

显然改变了所需输出的输入,FontStyle.Bold是这里的重要部分。

相关问题