Search code examples
c#.netrecursiontoolstrip

C# | How to get all child items of ToolStrip?


I need to get any child item of ToolStrip/MenuStrip/StatusStrip for translate the texts.

I did it with Controls by simple recursion but I don't know how to do that with ToolStrip items because there is not DropDownItems property in ToolStripItem class.


Solution

  • I written this and it does the work well.

    private ToolStripItem[] GetAllChildren(ToolStripItem item)
        {
            List<ToolStripItem> Items = new List<ToolStripItem> { item };
            if (item is ToolStripMenuItem)
                foreach (ToolStripItem i in ((ToolStripMenuItem)item).DropDownItems)
                    Items.AddRange(GetAllChildren(i));
            else if (item is ToolStripSplitButton)
                foreach (ToolStripItem i in ((ToolStripSplitButton)item).DropDownItems)
                    Items.AddRange(GetAllChildren(i));
            else if (item is ToolStripDropDownButton)
                foreach (ToolStripItem i in ((ToolStripDropDownButton)item).DropDownItems)
                    Items.AddRange(GetAllChildren(i));
            return Items.ToArray();
        }