Reputation: 369
I am using a MenuStrip
with two main ToolStripMenuItems
, each one of those has its "dropdown" with more ToolStripMenuItems that have the CheckOnClick
property set to true.
Now I am trying to retrieve the selected item, I know bool ToolStripMenuItem.Checked
exists, but how can I use a loop to get the ToolStripMenuItems
from each main ToolStripMenuItem
and then check which one has .Cheked is == true?
Or is there a better way to retrieve the checked ToolStripMenuItem
?
Upvotes: 3
Views: 6105
Reputation: 7563
Assume you are using Linq, here is what you can do:
private void button1_Click(object sender, EventArgs e)
{
foreach (var item in this.menuStrip1.Items.Cast<ToolStripMenuItem>())
{
GetCheckMenuItemText(item);
}
}
private void GetCheckMenuItemText(ToolStripMenuItem item)
{
if (item.HasDropDownItems)
{
foreach (var subItem in item.DropDownItems.Cast<ToolStripMenuItem>())
{
GetCheckMenuItemText(subItem);
}
}
else
{
if (item.CheckOnClick)
Debug.WriteLine(item.Text);
}
}
Upvotes: 3