Clear all radio buttons in a page

匿名 (未验证) 提交于 2019-12-03 02:26:02

问题:

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 button click. How can I do that?

回答1:

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


回答2:

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


回答3:

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.)



回答4:

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.



回答5:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!