How to enumerate the sub-items of menuStrip to merge the following two foreach blocks into one foreach block?

  • Thread starter Thread starter E-John
  • Start date Start date
E

E-John

Guest
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


1476396.png

Fig.1 Windows Form layout

1476397.png

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,
toolStripMenuItem1.DropDownItems.Name,
cult);
}

// languageToolStripMenuItem
for (int i = 0; i < languageToolStripMenuItem.DropDownItems.Count; i++)
{
resourcesss.ApplyResources(languageToolStripMenuItem.DropDownItems,
languageToolStripMenuItem.DropDownItems.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);
}

Continue reading...
 
Back
Top