簡體   English   中英

在WinForms中的MDI父窗體中循環瀏覽菜單項

[英]Loop Through Menu Items in MDI Parent Form in WinForms

我需要一種方法來遍歷MDI父窗體中的菜單項。

原因是因為我在激活按鈕時設置背景 ,以向用戶顯示他們選擇了哪些按鈕。

下圖顯示了在菜單中選擇System Settings的示例以及右側所示的child form

目前,我使用直接代碼實現此目的:

systemManagementToolStripMenuItem.BackColor = Color.Gray;

在此處輸入圖片說明

如何循環瀏覽,以便每次單擊菜單項時都會更改所選項目的背景顏色。

只要用戶執行啟動循環的操作,只需將ToolStipMenuItems收集到一個List並在列表上循環即可。

// First create the list of menu items
int selectedMenuItem = 0;
List<ToolStripMenuItem> menuItems = new List<ToolStripMenuItem>();
menuItems.Add(systemManagementToolStripMenuItem);

// When the user performs some action, such as pressing down arrow
selectedMenuItem = (selectedMenuItem + 1) % menuItems.Count;
UpdateSelectedItems();

// Have some method to update the buttons
public void UpdateSelectedItems()
{
    foreach(var item in menuItems)
        item.BackColor = Color.DarkGray;
    menuItems[selectedMenuItem].BackColor = Color.Gray;
}

暫無
暫無

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

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