Toolbar with a group of buttons

I would like to have a toolbar with some buttons on it (WinFroms / C # /. Net4). I would like the click button to be checked and all the others to be unchecked, so I want only the button with the button pressed to be pressed, and all the others not checked.

I know that the toolstrip button has a checkedonlclick property, but when I click one button, others can also be checked. In the good old VB6, there was an automatic solution to this problem: buttongroup on the toolbar. Are there any similar features in Winfoms?

Or should I process it from code ?! Switch all other buttons to unchecked when one button is checked? If so, then this will not be my favorite decision ...

+6
source share
4 answers

Call this code on toolStripButton_Click events and get the desired result.

foreach (ToolStripButton item in ((ToolStripButton)sender).GetCurrentParent().Items) { if (item == sender) item.Checked = true; if ((item != null) && (item != sender)) { item.Checked = false; } } 
+10
source

I think this is an old question, however, I was looking for a solution to this problem, and also created a kind of ToolStripRadioButton by expanding ToolStripButton. As far as I can see, the behavior should be the same as a regular switch. However, I added a group identifier so that you can have multiple radio button groups in the same toolbar.

You can add a switch to the toolbar, just like a regular ToolStripButton tool:

Add RadioButton

To make the button higher when it's installed, I gave it a background with a gradient (CheckedColor1 to CheckedColor2 from top to bottom):

Checked RadioButton gradient background

 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<ToolStripRadioButton>().Count(x => x.RadioButtonGroupId == RadioButtonGroupId && x.Checked); if (checkedCount > 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 && updateButtonGroup) { foreach (ToolStripRadioButton radioButton in this.Parent.Items.OfType<ToolStripRadioButton>()) { // Disable all other radio buttons with same group id if (radioButton != this && 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); } } 

Perhaps useful for others.

+2
source

I do not know how to do this in the designer, but it is quite simple to do this in code:

 readonly Dictionary<string, HashSet<ToolStripButton>> mButtonGroups; ... ToolStripButton AddGroupedButton(string pText, string pGroupName) { var newButton = new ToolStripButton(pText) { CheckOnClick = true }; mSomeToolStrip.Items.Add(newButton); HashSet<ToolStripButton> buttonGroup; if (!mButtonGroups.TryGetValue(pGroupName, out buttonGroup)) { buttonGroup = new HashSet<ToolStripButton>(); mButtonGroups.Add(pGroupName, buttonGroup); } newButton.Click += (s, e) => { foreach (var button in buttonGroup) button.Checked = button == newButton; }; return newButton; } 
+1
source

I did not do this a bit, but if you use a group box around the switches, it will only check once.

-1
source

Source: https://habr.com/ru/post/890778/


All Articles