winform menustrip的遍历

我这个是设置指定的菜单为不可用,是在权限控制的时候,我遍历了一下。 

 private void CheckSubMenu(ToolStripMenuItem menuItem, string MenuItemName)
        {
            if (menuItem.Text.Equals(MenuItemName))
            {
                menuItem.Enabled = false;
            }
            for (int i = 0; i < menuItem.DropDownItems.Count; i++)
            {
                if (menuItem.DropDownItems[i] is ToolStripSeparator)
                {
                    continue;
                }
                else
                {
                    CheckSubMenu((ToolStripMenuItem)menuItem.DropDownItems[i], MenuItemName);
                }
            }
        }

        private void CheckMenu(MenuStrip Menu,string MenuItemName)
        {
            foreach (ToolStripMenuItem n in Menu.Items)
            {
                CheckSubMenu(n,MenuItemName);
            }
        }

  红字部分,我本来是用的的foreach循环如下:

       foreach(ToolStripMenuItem  m    in   menuItem.DropDownItems)

         {

              CheckSubMenu(m,MenuItemName);

         }

  但一测试过后发现我的菜单里有ToolStripSeparator这个很讨厌,所以没办法就改成for循环,加了是否ToolStripSeparator的判断

 

posted on 2009-09-29 13:09  ewyb  阅读(1254)  评论(0编辑  收藏  举报

导航