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);
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.