0

There's a toolstripmenuitem in my Windows form Application. I need to access every sub menu items and check whether there is a specific menu item name is available and if that item found, I want to disable it. Eg:

     Report
         |__Stock
         |     |__Stock Balance
         |     |__Stock Reorder
         |__Sales
               |__Summary

My code is like this. According to my code, I can access sub menu(Stock) and disable it. But I'm unable to access child items(Stock Balance) inside sub menu.

String specificMenuItemName = "Stock Balance";

                    foreach (ToolStripMenuItem menuItem in MainMenuStrip.Items)
                    {
                        if (menuItem != null)
                        {
                            if (menuItem.HasDropDownItems)
                            {
                                foreach (ToolStripItem subMenuItem in menuItem.DropDownItems)
                                {
                                    if (subMenuItem is ToolStripSeparator)
                                    { }
                                    else
                                    {
                                        if (specificMenuItemName == subMenuItem.Text)
                                        {
                                            subMenuItem.Enabled = false;
                                        }
                                    }
                                }
                            }
                        }
                    }

How do I access to Stock Balance and disable it?

Kasun Perera
  • 400
  • 1
  • 2
  • 11

1 Answers1

2

What about a recursive function that walks down every item that has drop-down items until it finds the one with the specified name? something like this (quick-and-dirty, skipped checking for separators and stuff like that...):

private static void DisableItem(ToolStripDropDownItem menu, bool enable, string text)
{
    if (!menu.HasDropDownItems)
        if (Equals(menu.Text, text))
            menu.Enabled = enable;
        else
            return;

    foreach(var subItem in menu.DropDownItems)
    {
        var item = subItem as ToolStripDropDownItem;
        if (item == null) continue; 

        if (item.HasDropDownItems)
            DisableItem(item, enable, text);
        if (Equals(item.Text, text))
            item.Enabled = enable;
    }
}
Michael
  • 1,931
  • 2
  • 8
  • 22
  • @Mr.Michael, Your answer is really helpful for me. It's working well. But now I'm having a problem of using ToolStripSeparator. ToolStripSeparator is under ToolStripItem. I get an error, "Unable to cast object of type 'System.Windows.Forms.ToolStripSeparator' to type 'System.Windows.Forms.ToolStripMenuItem'." Can you modify your answer to check and avoid ToolStripSeperator? – Kasun Perera Aug 08 '17 at 09:49
  • 1
    @KasunPerera I've changed my code. Changed type of menu-parameter and added cast/type-checking inside the loop... `ToolStripDropDownItem` is the class that has the `.DropDownItems` property so we can ignore every menu-item that hasn't this property. If you have combo- or textboxes in your menu, they will also be ignored... – Michael Aug 09 '17 at 08:22
  • @Mr.Michael, I can solve my problem using your final suggestion. Thanks a lot. Really appreciate your contribution. – Kasun Perera Aug 09 '17 at 14:27