--- title: "How to: Display Option Buttons in a MenuStrip" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "MenuStrip [Windows Forms], displaying option buttons" - "displaying option buttons [Windows Forms], MenuStrip [Windows Forms]" - "option buttons [Windows Forms], displaying in MenuStrip" ms.assetid: 8b596af2-9ff8-4f7b-93d7-cba830e167f4 --- # How to: Display Option Buttons in a MenuStrip (Windows Forms) Option buttons, also known as radio buttons, are similar to check boxes except that users can select only one at a time. Although by default the class does not provide option-button behavior, the class does provide check-box behavior that you can customize to implement option-button behavior for menu items in a control. When the property of a menu item is `true`, users can click the item to toggle the display of a check mark. The property indicates the current state of the item. To implement basic option-button behavior, you must ensure that when an item is selected, you set the property for the previously selected item to `false`. The following procedures describe how to implement this and additional functionality in a class that inherits the class. The `ToolStripRadioButtonMenuItem` class overrides members such as and to provide the selection behavior and appearance of option buttons. Additionally, this class overrides the property so that options on a submenu are disabled unless the parent item is selected. ## To implement option-button selection behavior 1. Initialize the property to `true` to enable item selection. [!code-csharp[ToolStripRadioButtonMenuItem#110](~/samples/snippets/csharp/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/cs/ToolStripRadioButtonMenuItem.cs#110)] [!code-vb[ToolStripRadioButtonMenuItem#110](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/vb/ToolStripRadioButtonMenuItem.vb#110)] 2. Override the method to clear the selection of the previously selected item when a new item is selected. [!code-csharp[ToolStripRadioButtonMenuItem#120](~/samples/snippets/csharp/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/cs/ToolStripRadioButtonMenuItem.cs#120)] [!code-vb[ToolStripRadioButtonMenuItem#120](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/vb/ToolStripRadioButtonMenuItem.vb#120)] 3. Override the method to ensure that clicking an item that has already been selected will not clear the selection. [!code-csharp[ToolStripRadioButtonMenuItem#130](~/samples/snippets/csharp/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/cs/ToolStripRadioButtonMenuItem.cs#130)] [!code-vb[ToolStripRadioButtonMenuItem#130](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/vb/ToolStripRadioButtonMenuItem.vb#130)] ## To modify the appearance of the option-button items 1. Override the method to replace the default check-mark with an option button by using the class. [!code-csharp[ToolStripRadioButtonMenuItem#140](~/samples/snippets/csharp/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/cs/ToolStripRadioButtonMenuItem.cs#140)] [!code-vb[ToolStripRadioButtonMenuItem#140](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/vb/ToolStripRadioButtonMenuItem.vb#140)] 2. Override the , , , and methods to track the state of the mouse and ensure that the method paints the correct option-button state. [!code-csharp[ToolStripRadioButtonMenuItem#150](~/samples/snippets/csharp/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/cs/ToolStripRadioButtonMenuItem.cs#150)] [!code-vb[ToolStripRadioButtonMenuItem#150](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/vb/ToolStripRadioButtonMenuItem.vb#150)] ## To disable options on a submenu when the parent item is not selected 1. Override the property so that the item is disabled when it has a parent item with both a value of `true` and a value of `false`. [!code-csharp[ToolStripRadioButtonMenuItem#160](~/samples/snippets/csharp/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/cs/ToolStripRadioButtonMenuItem.cs#160)] [!code-vb[ToolStripRadioButtonMenuItem#160](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/vb/ToolStripRadioButtonMenuItem.vb#160)] 2. Override the method to subscribe to the event of the parent item. [!code-csharp[ToolStripRadioButtonMenuItem#170](~/samples/snippets/csharp/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/cs/ToolStripRadioButtonMenuItem.cs#170)] [!code-vb[ToolStripRadioButtonMenuItem#170](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/vb/ToolStripRadioButtonMenuItem.vb#170)] 3. In the handler for the parent-item event, invalidate the item to update the display with the new enabled state. [!code-csharp[ToolStripRadioButtonMenuItem#180](~/samples/snippets/csharp/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/cs/ToolStripRadioButtonMenuItem.cs#180)] [!code-vb[ToolStripRadioButtonMenuItem#180](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/vb/ToolStripRadioButtonMenuItem.vb#180)] ## Example The following code example provides the complete `ToolStripRadioButtonMenuItem` class, and a class and `Program` class to demonstrate the option-button behavior. [!code-csharp[ToolStripRadioButtonMenuItem#000](~/samples/snippets/csharp/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/cs/ToolStripRadioButtonMenuItem.cs#000)] [!code-vb[ToolStripRadioButtonMenuItem#000](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ToolStripRadioButtonMenuItem/vb/ToolStripRadioButtonMenuItem.vb#000)] ## Compiling the Code This example requires: - References to the System, System.Drawing, and System.Windows.Forms assemblies. ## See also - - - - - - - - - [MenuStrip Control](menustrip-control-windows-forms.md) - [How to: Implement a Custom ToolStripRenderer](how-to-implement-a-custom-toolstriprenderer.md)