簡體   English   中英

如何在ContextMenuStrip中設置默認菜單項?

[英]How do I set the default menu item in a ContextMenuStrip?

在我的應用程序中,我在右鍵單擊對象時使用彈出菜單項。 我使用以下代碼動態構建此菜單:

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

現在,我想以粗體(如Windows用戶體驗指南推薦)設置其中一個菜單項,以指示雙擊對象時哪個操作對應。

我該怎么做呢?

使用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是這里的重要部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM