Clear all radio buttons in a page

前端 未结 5 498
误落风尘
误落风尘 2021-01-13 13:08

I have lot of dynamically generated radio buttons in my Windows Forms project. They may be checked based on the values in a database. I want to clear all radio buttons in a

相关标签:
5条回答
  • 2021-01-13 13:37

    Both will do it below, as long as _radioContainer is a GroupBox.

    private void button1_Click(object sender, EventArgs e) {
    
        // This will remove the radiobuttons completely...
        _radioContainer.Controls.OfType<RadioButton>().ToList().ForEach(p => _radioContainer.Controls.Remove(p));
    
        // Either of the below will clear the checked state
        _radioContainer.Controls.OfType<RadioButton>().ToList().ForEach(p => p.Checked = false);
    
        foreach (RadioButton radio in _radioContainer.Controls.OfType<RadioButton>().ToList()) {
            if (radio.Checked == true) {
                radio.Checked = false;
                break;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-13 13:39

    I encountered a similar problem, where none of these other answers were useful.

    I wanted to initialize a Winform Dialog with 2 Radiobuttons, that should be both unchecked. The user has to make an explicit choice, before something is selected (like in this question: https://ux.stackexchange.com/questions/76181/radio-buttons-with-none-selected).

    Problem was: the first RadioButton (the one with the lower TabIndex) was always pre-checked. Manually unchecking one, just checked the other one (no matter if in constructor or at the Load Event).

    Resolution: set the TabStop property to false for both RadioButtons. Don't ask me why though.

    0 讨论(0)
  • 2021-01-13 13:42

    Check this:

    private void button1_Click(object sender, EventArgs e) 
    {
        var cntls = GetAll(this, typeof(RadioButton));
        foreach (Control cntrl in cntls)
        {
            RadioButton _rb = (RadioButton)cntrl;
            if (_rb.Checked)
            {
                _rb.Checked = false;
            }
        }
    }
    
    public IEnumerable<Control> GetAll(Control control, Type type)
    {
        var controls = control.Controls.Cast<Control>();
        return controls.SelectMany(ctrls => GetAll(ctrls, type)).Concat(controls).Where(c => c.GetType() == type);
    }
    
    0 讨论(0)
  • 2021-01-13 13:49

    I don't know if this is the case, but you may have radio buttons nested inside of other Controls. If that's the case you will need to go through all of the .Controls Collections of all of your controls in order to find them all and switch them off. You can use this helper function to do that:

        void ExecuteOnAllChildren<U>(Control c, Action<Control> T) where U : Control
        {
            c.Controls.OfType<U>().ToList().ForEach(a => T(a) );
    
            foreach(Control childControl in c.Controls)
                ExecuteOnAllChildren<U>(childControl, T);
    
        }
    

    Use it by saying:

        ExecuteOnAllChildren<RadioButton>(this, a => { a.Checked = false; });
    

    (I assume "this" is your Form. Otherwise replace "this" with the form that you would like to do all of the replaces with.)

    0 讨论(0)
  • 2021-01-13 13:59
    void Button1Click(object sender, EventArgs e)
    {
        foreach (Control ctrl in Controls)
        {
            if (ctrl is Panel)
            {
                foreach (Control rdb in ctrl.Controls)
                {
                    if (rdb is RadioButton && ((RadioButton)rdb).Checked == true)
                    {
                        ((RadioButton)rdb).Checked = false;
                    }
                }
            }
        }
    }
    

    This clears all the checked radio buttons on a button click.

    0 讨论(0)
提交回复
热议问题