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

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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>();
                if (item != null) childs.Add(item.Text);
                item = item.OwnerItem;

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

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
Aren't close automatically? Do you do some other things ? And do you want to close all open items in this case?
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

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();
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.
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();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
countrymeisterAuthor Commented:
Thank you so much, I have been struggling with this for the last two hours
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.