Best way to databind a group of radiobuttons in WinForms

前端 未结 9 564
无人共我
无人共我 2020-12-03 00:58

I\'m currently working on databinding some of my existing Windows Forms, and I\'ve ran into an issue figuring out the proper way of databinding a group of radiobutton contro

9条回答
  •  悲哀的现实
    2020-12-03 01:43

    I liked the idea of a RadioButtonGroupBox but I decided to create a version that is self supporting. There is no reason to add value to Tag attribute or to introduce new value attributes. Any assigned radio button is still a member of the RadioButtonGroupBox and the sequence of radiobuttons is defined during development. Soo, I modified the code. Now I can get and set the selected radiobutton by index position, By Control Name and by Text. BTW Text is only useable if your asssigned Text is different for each radiobutton.

    public class RadioButtonGroupBox : GroupBox
    {
        public event EventHandler SelectedChanged = delegate { };
    
        int _nIndexPosCheckRadioButton = -1;
        int _selected;
        public int Selected
        {
            get
            {
                return _selected;
            }
        }
    
    
        public int CheckedRadioButtonIndexPos
        {
            set
            {
                int nPosInList = -1;
                foreach (RadioButton item in this.Controls.OfType())
                {
                    // There are RadioButtonItems in the list...
                    nPosInList++;
    
                    // Set the RB that should be checked
                    if (nPosInList == value)
                    {
                        item.Checked = true;
                        // We can stop with the loop
                        break;
                    }
                }
                _nIndexPosCheckRadioButton = nPosInList;
            }
            get
            {
                int nPosInList = -1;
                int nPosCheckeItemInList = -1;
    
                foreach (RadioButton item in this.Controls.OfType())
                {
                    // There are RadioButtonItems in the list...
                    nPosInList++;
    
                    // Find the RB that is checked
                    if (item.Checked)
                    {
                        nPosCheckeItemInList = nPosInList;
                        // We can stop with the loop
                        break;
                    }
                }
                _nIndexPosCheckRadioButton = nPosCheckeItemInList;
                return _nIndexPosCheckRadioButton;
            }
        }
    
        public string CheckedRadioButtonByText
        {
            set
            {
                int nPosInList = -1;
                foreach (RadioButton item in this.Controls.OfType())
                {
                    // There are RadioButtonItems in the list...
                    nPosInList++;
    
                    // Set the RB that should be checked
                    if (item.Text == value)
                    {
                        item.Checked = true;
                        // We can stop with the loop
                        break;
                    }
                }
                _nIndexPosCheckRadioButton = nPosInList;
            }
            get
            {
                string cByTextValue = "__UNDEFINED__";
                int nPosInList = -1;
                int nPosCheckeItemInList = -1;
    
                foreach (RadioButton item in this.Controls.OfType())
                {
                    // There are RadioButtonItems in the list...
                    nPosInList++;
    
                    // Find the RB that is checked
                    if (item.Checked)
                    {
                        cByTextValue = item.Text;
                        nPosCheckeItemInList = nPosInList;
                        // We can stop with the loop
                        break;
                    }
                }
                _nIndexPosCheckRadioButton = nPosCheckeItemInList;
                return cByTextValue;
            }
        }
    
        public string CheckedRadioButtonByName
        {
            set
            {
                int nPosInList = -1;
                foreach (RadioButton item in this.Controls.OfType())
                {
                    // There are RadioButtonItems in the list...
                    nPosInList++;
    
                    // Set the RB that should be checked
                    if (item.Name == value)
                    {
                        item.Checked = true;
                        // We can stop with the loop
                        break;
                    }
                }
                _nIndexPosCheckRadioButton = nPosInList;
            }
            get
            {
                String cByNameValue = "__UNDEFINED__";
                int nPosInList = -1;
                int nPosCheckeItemInList = -1;
    
                foreach (RadioButton item in this.Controls.OfType())
                {
                    // There are RadioButtonItems in the list...
                    nPosInList++;
    
                    // Find the RB that is checked
                    if (item.Checked)
                    {
                        cByNameValue = item.Name;
                        nPosCheckeItemInList = nPosInList;
                        // We can stop with the loop
                        break;
                    }
                }
                _nIndexPosCheckRadioButton = nPosCheckeItemInList;
                return cByNameValue;
            }
        }
    
    
        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
    
            var radioButton = e.Control as RadioButton;
            if (radioButton != null)
                radioButton.CheckedChanged += radioButton_CheckedChanged;
        }
    
    
        void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            _selected = CheckedRadioButtonIndexPos;
            SelectedChanged(this, new EventArgs());
        }
    
    }
    

提交回复
热议问题