Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the ToolStripMenuItem child in WinForms

Tags:

c#

winforms

H all,

I created a menu strip in Winform not dynamically. And it's all in invisible, when the user is have rights only it visible. My one of username have full rights,. For this I wrote the below code,..

private void menuActive(MenuStrip menus)
{
     foreach (ToolStripMenuItem menu in menus.Items)
     {
          menu.Visible = true;               
          for (int i = 0; i < menu.DropDown.Items.Count; i++)
          {
               menu.DropDown.Items[i].Visible = true;                        
          }
     }
}

But this is visible the menuItem and child menuItem,. my few childItem menu have more childItem. That means, In TsmMaster and tsmregisterMaster are visible but I can't access the registerMasters Childs(ClassMaster, division Master....)

See the below image,..

enter image description here

Please give your suggestion.

like image 404
Sagotharan Avatar asked Feb 03 '12 06:02

Sagotharan


2 Answers

Try it with recursion:

private void ActivateMenus(ToolStripItemCollection items)
{
    foreach (ToolStripMenuItem item in items)
    {
        item.Visible = true;    
        ActivateMenus(item.DropDown.Items);
    }
}
like image 157
Denis Biondic Avatar answered Oct 21 '22 08:10

Denis Biondic


Fixed version of Your code

       private void menuActive(MenuStrip menus)
       {
            foreach (ToolStripMenuItem menu in menus.Items)
            {
                activateItems(menu);
            }
        }

        private void activateItems(ToolStripMenuItem item)
        {
            item.Visible = true;
            for (int i = 0; i < item.DropDown.Items.Count; i++)
            {
                ToolStripItem subItem = item.DropDown.Items[i];
                subItem.Visible = true;
                if (item is ToolStripMenuItem)
                {
                    activateItems(subItem as ToolStripMenuItem);
                }

            }
        }
like image 34
Thaven Avatar answered Oct 21 '22 08:10

Thaven