Iterarting menu item
-
when I iterate through form controls and their child controls it works fine for every thing else expect when it comes to menu items it only returns only the top item and not the child ones the code I am using is
void ModifyControls(Control.ControlCollection ControlCollection) { //iterator for (int i = 0; i < ControlCollection.Count; i++) { if (ControlCollection[i].GetType() != typeof(Menu)) { // add control Array.Resize(ref Controls, Controls.Length + 1); Controls[Controls.Length - 1] = new MyControls(ControlCollection[i]); // save control specific formating and apply it Controls[Controls.Length - 1].SaveFormating(); //itrate through child controls ModifyControls(ControlCollection[i].Controls); } } }
Thanks and Regards Amar Chaudhary
It is Good to be Important but! it is more Important to be Good
-
when I iterate through form controls and their child controls it works fine for every thing else expect when it comes to menu items it only returns only the top item and not the child ones the code I am using is
void ModifyControls(Control.ControlCollection ControlCollection) { //iterator for (int i = 0; i < ControlCollection.Count; i++) { if (ControlCollection[i].GetType() != typeof(Menu)) { // add control Array.Resize(ref Controls, Controls.Length + 1); Controls[Controls.Length - 1] = new MyControls(ControlCollection[i]); // save control specific formating and apply it Controls[Controls.Length - 1].SaveFormating(); //itrate through child controls ModifyControls(ControlCollection[i].Controls); } } }
Thanks and Regards Amar Chaudhary
It is Good to be Important but! it is more Important to be Good
Greetings Amar, If you look at how the MenuStrip control works you will notice that it houses a different type of collection, the ToolStripItemCollection in its MenuStrip.Items property. You need to iterate each ToolStripMenuItem in that collection to get its children. After that, the ToolStripMenuItem has another ToolStripItemCollection in it's DropDownItems property, you now need to iterate through this collection to get it's children and so forth... consider the following example:
void ListControls(Control parentControl) { foreach (Control control in parentControl.Controls) { MessageBox.Show("Control Type : " + control.GetType().ToString() + "\n" + "Has Children : " + (control.Controls.Count > 0 ? "Yes" : "No") + "\n"); // Perform the child-find functionality on the ControlCollection here. if (control.Controls.Count > 0) { // Call ourselves to discover additional children... this.ListControls(control); } // Get the sub menus of a MenuStrip. // * Test for the type of control, we're looking for the MenuStrip here... // TODO: Add this to a different function to nest-find child tool strips. if (control.GetType() == typeof(MenuStrip)) { // As you can see, the menu houses a collection of items not controls, notice this is now a ToolStripItemcollection not ControlCollection. foreach (ToolStripMenuItem toolStripMenuItem in ((MenuStrip)control).Items) { MessageBox.Show("---> Menu Strip '" + ((MenuStrip)control).Text + "' has '" + toolStripMenuItem.Text + "' as a child item."); // Test if tool strip has child items, notice this is a ToolStripItemcollection too. if (toolStripMenuItem.DropDownItems.Count > 0) { // Call your nested-find function... } } } } }
Basically, the MenuStrip needs to be treated like a TreeView as it can contain a complex hierarchy. I hope this helps... Have a great weekend!Fernando Mendes Senior .NET Developer, Architect
-
Greetings Amar, If you look at how the MenuStrip control works you will notice that it houses a different type of collection, the ToolStripItemCollection in its MenuStrip.Items property. You need to iterate each ToolStripMenuItem in that collection to get its children. After that, the ToolStripMenuItem has another ToolStripItemCollection in it's DropDownItems property, you now need to iterate through this collection to get it's children and so forth... consider the following example:
void ListControls(Control parentControl) { foreach (Control control in parentControl.Controls) { MessageBox.Show("Control Type : " + control.GetType().ToString() + "\n" + "Has Children : " + (control.Controls.Count > 0 ? "Yes" : "No") + "\n"); // Perform the child-find functionality on the ControlCollection here. if (control.Controls.Count > 0) { // Call ourselves to discover additional children... this.ListControls(control); } // Get the sub menus of a MenuStrip. // * Test for the type of control, we're looking for the MenuStrip here... // TODO: Add this to a different function to nest-find child tool strips. if (control.GetType() == typeof(MenuStrip)) { // As you can see, the menu houses a collection of items not controls, notice this is now a ToolStripItemcollection not ControlCollection. foreach (ToolStripMenuItem toolStripMenuItem in ((MenuStrip)control).Items) { MessageBox.Show("---> Menu Strip '" + ((MenuStrip)control).Text + "' has '" + toolStripMenuItem.Text + "' as a child item."); // Test if tool strip has child items, notice this is a ToolStripItemcollection too. if (toolStripMenuItem.DropDownItems.Count > 0) { // Call your nested-find function... } } } } }
Basically, the MenuStrip needs to be treated like a TreeView as it can contain a complex hierarchy. I hope this helps... Have a great weekend!Fernando Mendes Senior .NET Developer, Architect
thanks for the help Amar Chaudhary
It is Good to be Important but! it is more Important to be Good