2011-06-17 17 views
6

Chciałbym mieć pasek narzędziowy z niektórymi przyciskami (WinFroms/C#/.net4). Chciałbym, aby kliknięty przycisk był zaznaczony, a wszystkie inne niezaznaczone, więc chcę mieć zaznaczony tylko przycisk kliknięcia, wszystkie pozostałe są odznaczone.Pasek narzędziowy z grupą przycisków wyboru

Wiem, że przycisk paska narzędzi ma właściwość checkedonlclick, ale po kliknięciu jednego przycisku można również sprawdzić inne. W starym dobrym VB6 było automatyczne rozwiązanie tego problemu: buttongroup na pasku narzędzi. Czy jest coś podobnego w Winfoms?

Czy powinienem sobie z tym poradzić z kodem ?! Przełączyć wszystkie pozostałe przyciski, aby odznaczać stan, gdy zaznaczony jest jeden przycisk? Jeśli tak, to nie byłoby to moje ulubione rozwiązanie ...

Odpowiedz

10

Zadzwoń pod ten kod na zdarzenia toolStripButton_Click, a otrzymasz oczekiwany wynik.

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

Thx, wiem, że ... Myślałem, że istnieje prostsze rozwiązanie dla paska narzędzi ... – Tom

-1

Nie zrobiłem tego w niewielkim stopniu, ale jeśli używasz pola grupowego wokół przycisków opcji, zezwalasz tylko na sprawdzanie w tym samym czasie.

+1

Nie są to zwykłe pola wyboru, są to przyciski na pasku narzędzi na taśmie stołowej – Tom

1

nie jestem świadomy sposób, aby to zrobić w projektanta, ale jest to dość proste do wykonania w kodzie:

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

Chyba jest to stara sprawa, jednak szukałem dla rozwiązania tego problemu, a skończyło się na zrobieniu pewnego rodzaju ToolStripRadioButton przez rozszerzenie ToolStripButton. Zachowanie powinno być takie samo, jak normalny przycisk radiowy, o ile widzę. Dodałem jednak identyfikator grupy, aby możliwe było posiadanie wielu grup przycisków radiowych w obrębie tego samego paska narzędziowego.

Można dodać przełącznik do ToolStrip jak normalny ToolStripButton:

Add RadioButton

Aby uczynić przycisk stand bardziej, gdy sprawdzana dałem mu tło gradientu (CheckedColor1 do CheckedColor2 od góry do dolny)

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

Może przydatne do innych.

Powiązane problemy