BLOG.CSHARPHELPER.COM: Make the items in a menu behave as a radio button group in C#
Make the items in a menu behave as a radio button group in C#
This example makes the items in a menu behave like a radio button group so when you select one, it displays a check mark and the others don't.
The key is the CheckMenuItem function.
This function takes as parameters a menu and a menu item. It loops through the objects contained in the menu. If an object is a menu item (e.g. not a separator or something else), the code sets its Checked property appropriately.
// Check this menu item and uncheck the others. private void mnuViewOption_Click(object sender, EventArgs e) { // Check the menu item. ToolStripMenuItem item = sender as ToolStripMenuItem; CheckMenuItem(mnuView, item);
// Do something with the menu selection. // You could use a switch statement here. // This example just displays the menu item's text. MessageBox.Show(item.Text); }
Comments