Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2007-09-09
7
Medium Priority
?
1,926 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:countrymeister
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:so3
ID: 19856616
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
 
LVL 1

Author Comment

by:countrymeister
ID: 19857088
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
 
LVL 6

Expert Comment

by:so3
ID: 19857120
Aren't close automatically? Do you do some other things ? And do you want to close all open items in this case?
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 6

Expert Comment

by:so3
ID: 19857172
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
 
LVL 1

Author Comment

by:countrymeister
ID: 19857198
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
 
LVL 6

Accepted Solution

by:
so3 earned 2000 total points
ID: 19857222
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
 
LVL 1

Author Comment

by:countrymeister
ID: 19857389
Thank you so much, I have been struggling with this for the last two hours
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question