Search code examples
c#toolstripmenu

Capturing whole ToolStripMenu tree with ToString


If I run the code in varying areas of my menu tree, I only get the one element, how would you firstly apply this logic to all sub components of this menu tree and secondly, illustrate the whole tree.

The code I have only shows 1 stage of each area applied

  MessageBox.Show((ToolStripMenuItem).ToString());

So the above would only show File or Save or Open, rather than File Open or File Save.

Should I be using a foreach with my toolstripmenuitems?


Solution

  • In the end I used a logic around the following syntax, then building up the string at the end

    ToolStripMenuItem ThisMenuItem = (ToolStripMenuItem)sender;
    string WhatClicked = ThisMenuItem.ToString();
    ToolStripMenuItem ThisMenuItemOwnerItem = (ToolStripMenuItem)(ThisMenuItem.GetCurrentParent() as ToolStripDropDown).OwnerItem;
    

    Then you can obviously get deeper with

    ToolStripMenuItem ThisOwnersOwnerItem = (ToolStripMenuItem)(ThisMenuItemOwnerItem.GetCurrentParent() as ToolStripDropDown).OwnerItem;
    

    and so forth adding checks to avoid null exceptions.