* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
7.9 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||
|---|---|---|---|---|---|---|---|---|---|
| How to: Display Option Buttons in a MenuStrip | 03/30/2017 |
|
|
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 xref:System.Windows.Forms.ToolStripMenuItem 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 xref:System.Windows.Forms.MenuStrip control.
When the xref:System.Windows.Forms.ToolStripMenuItem.CheckOnClick%2A property of a menu item is true, users can click the item to toggle the display of a check mark. The xref:System.Windows.Forms.ToolStripMenuItem.Checked%2A 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 xref:System.Windows.Forms.ToolStripMenuItem.Checked%2A 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 xref:System.Windows.Forms.ToolStripMenuItem class. The ToolStripRadioButtonMenuItem class overrides members such as xref:System.Windows.Forms.ToolStripMenuItem.OnCheckedChanged%2A and xref:System.Windows.Forms.ToolStripMenuItem.OnPaint%2A to provide the selection behavior and appearance of option buttons. Additionally, this class overrides the xref:System.Windows.Forms.ToolStripMenuItem.Enabled%2A property so that options on a submenu are disabled unless the parent item is selected.
To implement option-button selection behavior
-
Initialize the xref:System.Windows.Forms.ToolStripMenuItem.CheckOnClick%2A property to
trueto enable item selection.[!code-csharpToolStripRadioButtonMenuItem#110] [!code-vbToolStripRadioButtonMenuItem#110]
-
Override the xref:System.Windows.Forms.ToolStripMenuItem.OnCheckedChanged%2A method to clear the selection of the previously selected item when a new item is selected.
[!code-csharpToolStripRadioButtonMenuItem#120] [!code-vbToolStripRadioButtonMenuItem#120]
-
Override the xref:System.Windows.Forms.ToolStripMenuItem.OnClick%2A method to ensure that clicking an item that has already been selected will not clear the selection.
[!code-csharpToolStripRadioButtonMenuItem#130] [!code-vbToolStripRadioButtonMenuItem#130]
To modify the appearance of the option-button items
-
Override the xref:System.Windows.Forms.ToolStripMenuItem.OnPaint%2A method to replace the default check-mark with an option button by using the xref:System.Windows.Forms.RadioButtonRenderer class.
[!code-csharpToolStripRadioButtonMenuItem#140] [!code-vbToolStripRadioButtonMenuItem#140]
-
Override the xref:System.Windows.Forms.ToolStripMenuItem.OnMouseEnter%2A, xref:System.Windows.Forms.ToolStripMenuItem.OnMouseLeave%2A, xref:System.Windows.Forms.ToolStripMenuItem.OnMouseDown%2A, and xref:System.Windows.Forms.ToolStripMenuItem.OnMouseUp%2A methods to track the state of the mouse and ensure that the xref:System.Windows.Forms.ToolStripMenuItem.OnPaint%2A method paints the correct option-button state.
[!code-csharpToolStripRadioButtonMenuItem#150] [!code-vbToolStripRadioButtonMenuItem#150]
To disable options on a submenu when the parent item is not selected
-
Override the xref:System.Windows.Forms.ToolStripMenuItem.Enabled%2A property so that the item is disabled when it has a parent item with both a xref:System.Windows.Forms.ToolStripMenuItem.CheckOnClick%2A value of
trueand a xref:System.Windows.Forms.ToolStripMenuItem.Checked%2A value offalse.[!code-csharpToolStripRadioButtonMenuItem#160] [!code-vbToolStripRadioButtonMenuItem#160]
-
Override the xref:System.Windows.Forms.ToolStripMenuItem.OnOwnerChanged%2A method to subscribe to the xref:System.Windows.Forms.ToolStripMenuItem.CheckedChanged event of the parent item.
[!code-csharpToolStripRadioButtonMenuItem#170] [!code-vbToolStripRadioButtonMenuItem#170]
-
In the handler for the parent-item xref:System.Windows.Forms.ToolStripMenuItem.CheckedChanged event, invalidate the item to update the display with the new enabled state.
[!code-csharpToolStripRadioButtonMenuItem#180] [!code-vbToolStripRadioButtonMenuItem#180]
Example
The following code example provides the complete ToolStripRadioButtonMenuItem class, and a xref:System.Windows.Forms.Form class and Program class to demonstrate the option-button behavior.
[!code-csharpToolStripRadioButtonMenuItem#000] [!code-vbToolStripRadioButtonMenuItem#000]
Compiling the Code
This example requires:
- References to the System, System.Drawing, and System.Windows.Forms assemblies.
See also
- xref:System.Windows.Forms.MenuStrip
- xref:System.Windows.Forms.ToolStripMenuItem
- xref:System.Windows.Forms.ToolStripMenuItem.CheckOnClick%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.ToolStripMenuItem.Checked%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.ToolStripMenuItem.OnCheckedChanged%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.ToolStripMenuItem.OnPaint%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.ToolStripMenuItem.Enabled%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.RadioButtonRenderer
- MenuStrip Control
- How to: Implement a Custom ToolStripRenderer