2011-06-17 8 views
6

साथ Toolstrip मैं इसे (WinFroms/सी # /। नेट 4) पर कुछ बटन के साथ एक toolstrip करना चाहते हैं। मैं क्लिक किया जाने वाला बटन की जाँच की और अन्य सभी अनियंत्रित हो करना चाहते हैं, तो मैं केवल क्लिक किए गए बटन की जाँच की, सभी दूसरों अनियंत्रित करना चाहते हैं।जांच बटन समूह

मैं toolstrip बटन checkedonlclick संपत्ति है पता है, लेकिन जब मैं एक बटन क्लिक करें, दूसरों को भी जाँच की जा सकती है। अच्छे पुराने वीबी 6 में इस समस्या के लिए एक स्वचालित समाधान था: टूलबार पर buttongroup। Winfoms में कोई समान है?

या मैं यह कोड से संभाल चाहिए ?! एक बटन चेक किए जाने पर सभी अन्य बटन अनचेक किए गए राज्य पर स्विच करें? यदि हां, तो फिर यह मेरा पसंदीदा समाधान ...

उत्तर

10

कॉल toolStripButton_Click घटनाओं पर इस कोड को और आप इच्छित परिणाम प्राप्त करना चाहिए।

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

थक्स पर टूलस्ट्रिप बटन हैं, मुझे पता है कि ... मैंने सोचा था कि टूलस्ट्रिप के लिए एक और अधिक सरल समाधान था ... – Tom

-1

मैं इसे एक छोटे में नहीं किया है, लेकिन यदि आप रेडियो बटन के चारों ओर एक समूह बॉक्स का उपयोग करें यह केवल एक एक समय में जाँच की जा करने की अनुमति देगा नहीं होगा।

+1

वे नियमित चेकबॉक्स नहीं हैं, वे स्टूलस्ट्रिप – Tom

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 की तरह एक toolstrip रेडियो बटन जोड़ सकते हैं एक:

Add RadioButton

बटन खड़े बनाने के बाहर और अधिक जब जाँच की मैं इसे एक ढाल पृष्ठभूमि (ऊपर से करने के लिए CheckedColor2 को CheckedColor1 दे दिया है नीचे):

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

शायद रूप में अच्छी तरह दूसरों के लिए उपयोगी।

संबंधित मुद्दे