Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I guess this is an old question, however I was looking for a solution to this problem as well and ended up making a kind of ToolStripRadioButton by extending ToolStripButton. The behaviour should be the same as a normal radio button, as far as I can see. I have however added a group id to make it possible to have multiple radio button groups within the same toolstrip. </p> <p>One can add the radio button to a toolstrip like a normal ToolStripButton:</p> <p><img src="https://i.stack.imgur.com/61O6b.png" alt="Add RadioButton"></p> <p>To make the button stand more out when checked I have given it a gradient background (CheckedColor1 to CheckedColor2 from top to bottom):</p> <p><img src="https://i.stack.imgur.com/s40WX.png" alt="Checked RadioButton gradient background"></p> <pre><code>using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Windows.Forms; public class ToolStripRadioButton : ToolStripButton { private int radioButtonGroupId = 0; private bool updateButtonGroup = true; private Color checkedColor1 = Color.FromArgb(71, 113, 179); private Color checkedColor2 = Color.FromArgb(98, 139, 205); public ToolStripRadioButton() { this.CheckOnClick = true; } [Category("Behavior")] public int RadioButtonGroupId { get { return radioButtonGroupId; } set { radioButtonGroupId = value; // Make sure no two radio buttons are checked at the same time UpdateGroup(); } } [Category("Appearance")] public Color CheckedColor1 { get { return checkedColor1; } set { checkedColor1 = value; } } [Category("Appearance")] public Color CheckedColor2 { get { return checkedColor2; } set { checkedColor2 = value; } } // Set check value without updating (disabling) other radio buttons in the group private void SetCheckValue(bool checkValue) { updateButtonGroup = false; this.Checked = checkValue; updateButtonGroup = true; } // To make sure no two radio buttons are checked at the same time private void UpdateGroup() { if (this.Parent != null) { // Get number of checked radio buttons in group int checkedCount = this.Parent.Items.OfType&lt;ToolStripRadioButton&gt;().Count(x =&gt; x.RadioButtonGroupId == RadioButtonGroupId &amp;&amp; x.Checked); if (checkedCount &gt; 1) { this.Checked = false; } } } protected override void OnClick(EventArgs e) { base.OnClick(e); this.Checked = true; } protected override void OnCheckedChanged(EventArgs e) { if (this.Parent != null &amp;&amp; updateButtonGroup) { foreach (ToolStripRadioButton radioButton in this.Parent.Items.OfType&lt;ToolStripRadioButton&gt;()) { // Disable all other radio buttons with same group id if (radioButton != this &amp;&amp; radioButton.RadioButtonGroupId == this.RadioButtonGroupId) { radioButton.SetCheckValue(false); } } } } protected override void OnPaint(PaintEventArgs e) { if (this.Checked) { var checkedBackgroundBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), CheckedColor1, CheckedColor2); e.Graphics.FillRectangle(checkedBackgroundBrush, new Rectangle(new Point(0, 0), this.Size)); } base.OnPaint(e); } } </code></pre> <p>Perhaps useful to others as well.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload