Answered by:
How to enumerate the sub-items of menuStrip to merge the following two foreach loops into one foreach loop?

Question
-
Dear All,
The application is a demo of change multilingual when button is pressed.
The Fig.1 is the layout of Windows Form application and Fig.2 is the layout viewed by document outline.
From the document outline, we have two major control, one is tab control which contain some buttons.
And one menuStrip control.
The buttons are contained in the tab control. There is a ChangeControlTextRecursive() method to handle this.
To the menuStrip which contain two ToolStripMenuItems, toolStripMenuItem1 and languageStripMenuItem. Some ToolStripMenuItems are contained to toolStripMenuItem1 and languageStripMenuItem.
How to enumerate the sub-items of menuStrip to merge the following two foreach block into one foreach?
Thanks and Best regards,
E-John
Fig.1 Windows Form layout
Fig.2 Windows Form layout viewed by document outline
public void ChangeControlTextRecursive(ComponentResourceManager resources, CultureInfo cult, Control control) { if (control.Controls.Count != 0) { foreach (Control ctrl in control.Controls) { resources.ApplyResources(ctrl, ctrl.Name, cult); ChangeControlTextRecursive(resources, cult, ctrl); } } } private void LoadApplicationUI(string cultureCode) { ComponentResourceManager resourcesss = new ComponentResourceManager(typeof(Form1)); CultureInfo cult = new CultureInfo(cultureCode); // tab page foreach (Control control in this.Controls) { resourcesss.ApplyResources(control, control.Name, cult); ChangeControlTextRecursive(resourcesss, cult, control); } // toolStripMenuItem1 for (int i = 0; i < toolStripMenuItem1.DropDownItems.Count; i++) { resourcesss.ApplyResources(toolStripMenuItem1.DropDownItems[i], toolStripMenuItem1.DropDownItems[i].Name, cult); } // languageToolStripMenuItem for (int i = 0; i < languageToolStripMenuItem.DropDownItems.Count; i++) { resourcesss.ApplyResources(languageToolStripMenuItem.DropDownItems[i], languageToolStripMenuItem.DropDownItems[i].Name, cult); } } private void englishToolStripMenuItem_Click(object sender, EventArgs e) { neutralCulture = "en"; LoadApplicationUI(neutralCulture); } private void chineseTraditionalToolStripMenuItem_Click(object sender, EventArgs e) { neutralCulture = "zh-Hant"; LoadApplicationUI(neutralCulture); }
- Edited by E-John Wednesday, August 28, 2019 1:16 PM
Wednesday, August 28, 2019 2:54 AM
Answers
-
Greetings E-John.
Do you mean like this? (Assuming you meant 'for loops' not 'foreach loops'.)
foreach (ToolStripMenuItem topLevelItem in MenuStrip.Items) { foreach (ToolStripMenuItem item in topLevelItem.DropDownItems) { // Do your stuff here. } }
DropDownItems can have their own DropDownItems, so you should probably make the inner loop recursive, but that's the idea.
- Marked as answer by E-John Wednesday, August 28, 2019 4:59 AM
Wednesday, August 28, 2019 3:40 AM
All replies
-
Greetings E-John.
Do you mean like this? (Assuming you meant 'for loops' not 'foreach loops'.)
foreach (ToolStripMenuItem topLevelItem in MenuStrip.Items) { foreach (ToolStripMenuItem item in topLevelItem.DropDownItems) { // Do your stuff here. } }
DropDownItems can have their own DropDownItems, so you should probably make the inner loop recursive, but that's the idea.
- Marked as answer by E-John Wednesday, August 28, 2019 4:59 AM
Wednesday, August 28, 2019 3:40 AM -
Dear Ante,
Do you mean like this? (Assuming you meant 'for loops' not 'foreach loops'.)
---> Yes.
It works after applying provided code. Thanks for your helps.
Best regards,
E-John
foreach (ToolStripMenuItem topLevelItem in menuStrip1.Items) { resourcesss.ApplyResources(topLevelItem, topLevelItem.Name, cult); foreach (ToolStripMenuItem item in topLevelItem.DropDownItems) { resourcesss.ApplyResources(item, item.Name, cult); } }
Wednesday, August 28, 2019 4:59 AM