【问题标题】:Toolstrip with check button group带有复选按钮组的工具条
【发布时间】:2011-09-17 09:33:10
【问题描述】:

我想要一个带有一些按钮的工具条 (WinFroms/c#/.net4)。我想选中单击的按钮而未选中所有其他按钮,因此我只想选中单击的按钮,未选中所有其他按钮。

我知道工具条按钮有checkedonlclick 属性,但是当我单击一个按钮时,其他按钮也可能会被选中。在旧的 VB6 中有一个自动解决这个问题的方法:工具栏上的按钮组。 Winfoms 有类似的吗?

或者我应该从代码中处理它?!选中一个按钮时将所有其他按钮切换为未选中状态?如果是这样,那它就不是我最喜欢的解决方案了……

【问题讨论】:

    标签: c# .net winforms toolstrip


    【解决方案1】:

    我不知道在设计器中执行此操作的方法,但在代码中执行此操作相当简单:

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

    【讨论】:

      【解决方案2】:

      在 toolStripButton_Click 事件上调用此代码,您应该会得到想要的结果。

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

      【讨论】:

      • 谢谢,我知道...我认为工具条有更直接的解决方案...
      【解决方案3】:

      我想这是一个老问题,但是我也在寻找解决这个问题的方法,最终通过扩展 ToolStripButton 制作了一种 ToolStripRadioButton。据我所知,该行为应该与普通单选按钮相同。然而,我添加了一个组 ID,以便在同一个工具条中拥有多个单选按钮组。

      可以像普通 ToolStripButton 一样将单选按钮添加到工具条中:

      为了使按钮在选中时更加突出,我给它一个渐变背景(从上到下从 CheckedColor1 到 CheckedColor2):

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

      也许对其他人也有用。

      【讨论】:

        【解决方案4】:

        我还没有做过,但是如果你在单选按钮周围使用一个组合框,它一次只允许选中一个。

        【讨论】:

        • 它们不是普通的复选框,它们是凳子上的工具条按钮
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-19
        • 2015-11-12
        • 2017-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多