Sagotharan
Sagotharan

Reputation: 2626

Access the ToolStripMenuItem child in 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.

Upvotes: 5

Views: 11700

Answers (2)

Thaven
Thaven

Reputation: 1897

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);
                }

            }
        }

Upvotes: 2

Denis Biondic
Denis Biondic

Reputation: 8201

Try it with recursion:

private void ActivateMenus(ToolStripItemCollection items)
{
    foreach (ToolStripMenuItem item in items)
    {
        item.Visible = true;    
        ActivateMenus(item.DropDown.Items);
    }
}

Upvotes: 3

Related Questions