[英]Loop Through Menu Items in MDI Parent Form in WinForms
I need a way to loop through the menu items in a MDI Parent Form . 我需要一种方法来遍历MDI父窗体中的菜单项。
The reason why is because I am setting the background of the buttons when they are activated to show the user which ones they have selected . 原因是因为我在激活按钮时设置背景 ,以向用户显示他们选择了哪些按钮。
The pictures below illustrates an example of System Settings
being selected in the menu and the child form
shown on the right. 下图显示了在菜单中选择
System Settings
的示例以及右侧所示的child form
。
Currently I achieve this using direct code: 目前,我使用直接代码实现此目的:
systemManagementToolStripMenuItem.BackColor = Color.Gray;
How can I loop through so that each time I click on a menu item it will change the background colour of the selected item. 如何循环浏览,以便每次单击菜单项时都会更改所选项目的背景颜色。
Just collect the ToolStipMenuItems
into a List
and loop over the list whenever the user performs the action to initiate the looping. 只要用户执行启动循环的操作,只需将
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.