7

いくつかのボタンが付いたツールストリップ(WinFroms / c#/。net4)が欲しいのですが。クリックしたボタンをオンにし、他のすべてのチェックを外したいので、クリックしたボタンだけをチェックし、他のすべてのボタンをオフにします。

ツールストリップボタンにcheckedonlclickプロパティがあることは知っていますが、1つのボタンをクリックすると、他のボタンもチェックされる場合があります。古き良きVB6には、この問題の自動解決策がありました。ツールバーのボタングループです。Winfomsに同様のものはありますか?

または、コードから処理する必要がありますか?!1つのボタンがチェックされているときに、他のすべてのボタンをオフの状態に切り替えますか?もしそうなら、それは私のお気に入りの解決策ではないでしょう...

4

4 に答える 4

12

toolStripButton_Clickイベントでこのコードを呼び出すと、目的の結果が得られるはずです。

   foreach (ToolStripButton item in ((ToolStripButton)sender).GetCurrentParent().Items)
   {
       if (item == sender) item.Checked = true;
       if ((item != null) && (item != sender))
       {
          item.Checked = false;
       }
   }
于 2011-06-17T20:03:31.337 に答える
3

これは古い質問だと思いますが、私もこの問題の解決策を探していて、ToolStripButtonを拡張して一種のToolStripRadioButtonを作成することになりました。私が見る限り、動作は通常のラジオボタンと同じである必要があります。ただし、同じツールストリップ内に複数のラジオボタングループを含めることができるように、グループIDを追加しました。

通常のToolStripButtonのように、ラジオボタンをツールストリップに追加できます。

RadioButtonを追加します

チェックしたときにボタンをより目立たせるために、グラデーションの背景(上から下への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);
    }
}

おそらく他の人にも役立つでしょう。

于 2013-08-28T09:50:39.393 に答える
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;
}
于 2011-07-14T17:59:00.090 に答える
-2

少しはしていませんが、ラジオボタンの周りにグループボックスを使用すると、一度に1つしかチェックできなくなります。

于 2011-06-17T18:05:19.223 に答える