countrymeister
asked on
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
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
ASKER
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
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?
You can use HideDropDown method to close them but how do you know were to launch this.
void child_item_DropDownItemCli cked(objec t sender, ToolStripItemClickedEventA rgs e)
{
ToolStripMenuItem item = e.ClickedItem as ToolStripMenuItem;
if (item != null && item.HasDropDownItems) item.HideDropDown();
}
void child_item_DropDownItemCli
{
ToolStripMenuItem item = e.ClickedItem as ToolStripMenuItem;
if (item != null && item.HasDropDownItems) item.HideDropDown();
}
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much, I have been struggling with this for the last two hours
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;
}