A ToolStripMenuItem and a CheckOne nice addition to the .NET 2.0 framework is the ToolStripMenuItem Class. The ToolStripMenuItem Class replaces the MenuItem Class. The ToolStripMenuItem has a Checked property that gets or sets a value indicating if the item is checked or not. Coupled with the ToolStripMenuItem.CheckOnClick property, this can be useful for identifying which item has been selected or activated. Unfortunately, there isn't some sort of 'allow only one checked' property which allows for only one item in a menu to be 'Checked' at a time. This functionality can be easily added in code (or you could derive your own class that has this functionality; a nice addition to a personal control library). Create an OnClick and OnCheckChanged method that are referenced by each of the ToolStripMenuItems. Basically, you check to see if the current item is checked or not and then toggle the check for the other menu items. You could also check another property of the sender (such as the Tag property) to perform some other action.
Here is an example:
private void CheckedIconsToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
...{
if (sender is ToolStripMenuItem)
...{
if (!((ToolStripMenuItem)sender).Checked) return;
foreach (ToolStripMenuItem item in (((ToolStripMenuItem)sender).GetCurrentParent().Items))
...{
if (item != null && item != sender && item.Checked)
...{
item.Checked = false;
return;
}
}
}
}
private void CheckedToolStripMenuItem_Click(object sender, EventArgs e)
...{
if (sender is System.Windows.Forms.ToolStripMenuItem)
...{
if (!((ToolStripMenuItem)sender).Checked)
...{
((ToolStripMenuItem)sender).Checked = !((ToolStripMenuItem)sender).Checked;
};
switch (((ToolStripMenuItem)sender).Tag.ToString())
...{
case "1":
break;
case "2":
break;
case "3":
break;
case "4":
break;
case "5":
break;
default:
break;
};
}
}