Menustrip window forms - how do I find the hierarchy of menu items that lead to the menu item clicked and close the dropdownitems

how do I find the hierarchy of menu items that lead to the menu item clicked and close the dropdownitems
For eaxmple , I have a menustrip with toolstripmenuitems, which in turn have toolstripmenuitems.

Once I have clicked on the menuitem of choice, I need the item that was clicked alongwith the parent, grand parent.

Example menustrip test has two menu items

American
European,
under American we have Chrysler, GM, Ford,
under Chrysler we have Auto, Trucks
under Trucks say we have red, green bue

If the user clicked green under Trucks, I need the hierarchy as American, Chrysler,Trucks, Red
If the user clciked just Chrysler, then I need to retrieve American,Chrysler
LVL 1
countrymeisterAsked:
Who is Participating?
 
so3Connect With a Mentor Commented:
k I understood now. try this

void child_item_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            ToolStripMenuItem parent_item = e.ClickedItem as ToolStripMenuItem;
            ToolStripMenuItem current_item = e.ClickedItem as ToolStripMenuItem;
            while (parent_item.OwnerItem != null)
            {
                parent_item = parent_item.OwnerItem as ToolStripMenuItem;
            }

            if (parent_item!=null && parent_item != current_item)
            {
                if (current_item != null && current_item.HasDropDownItems) parent_item.HideDropDown();
            }
        }
0
 
so3Commented:
Put the method GetPath in the dropdownitem event like this: Console.WriteLine(GetPath(e.ClickedItem));

        private string GetPath(ToolStripItem item)
        {
            List<string> childs = new List<string>();
            do
            {
                if (item != null) childs.Add(item.Text);
                item = item.OwnerItem;

            } while (item != null);
            childs.Reverse();
            string _hierarchy = "";
            foreach (string str in childs)
            {
                if (_hierarchy.Equals("")) _hierarchy += str;
                else _hierarchy += @", "+str;
            }
            return _hierarchy;
        }
0
 
countrymeisterAuthor Commented:
so3,

Thanks for that awesome logic, just the second part of my question how do I close all those dropdown items after the user has clicked the menuitem
For example if the user navigates to the menu item Chrysler, it shows auto, truck as they are the drop down items, and if the user clicks Chrysler, I get the right hierarchy(american, chrysler)  but the menu does not fold to the root menustrip, in this example test. it remains open on the form.
I want to fold all the items
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
so3Commented:
Aren't close automatically? Do you do some other things ? And do you want to close all open items in this case?
0
 
so3Commented:
You can use HideDropDown method to close them but how do you know were to launch this.


  void child_item_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            ToolStripMenuItem item = e.ClickedItem as ToolStripMenuItem;
            if (item != null && item.HasDropDownItems) item.HideDropDown();
        }
0
 
countrymeisterAuthor Commented:
I have a depth of four for my menu items, only when the last depth is clicked the items fold to the root
So if an item is clicked which has no more drop down itmes, the menu folds automatically
In all other cases, if I select an item which has leaves(drop down items) it just stays on the window until I click some other control.
these menu items are created at runtime form an xml file.

I want to close all items and fold to the root menustrip test.
I am not doing anything specific just assigning text and values to the items from the xml.
0
 
countrymeisterAuthor Commented:
Thank you so much, I have been struggling with this for the last two hours
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.