2

I have a program written in C# with a clear button, which needs to clear the content of the entire form. For the button I used on foreach for the radiobuttons and one for the checkboxes.

foreach (RadioButton rad in quiztabs.TabPages)
{
    rad.Checked = false;
}
foreach (CheckBox chk in quiztabs.TabPages)
{
    chk.Checked = false;
}

However, when I click the clear button I get an Unable to cast object of type System.Windows.Forms.TabPage to type System.Windows.Forms.RadioButton.

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
Oliver
  • 821
  • 5
  • 12
  • 28

7 Answers7

19

You misunderstand the behaviour of foreach: It won't filter all elements in TabPages, it will try to cast all of them.

If you want to filter, you can do so explicitly using LINQ:

    foreach (RadioButton rad in quiztabs.TabPages.OfType<RadioButton>())
    {
        rad.Checked = false;
    }
    foreach (CheckBox chk in quiztabs.TabPages.OfType<CheckBox>())
    {
        chk.Checked = false;
    }

However, this still won't solve your problem, since the TabPages collection only contains TabPage elements. What you probably want is something like this:

foreach (TabPage page in quiztabs.TabPages) 
{
    foreach (RadioButton rad in page.Controls.OfType<RadioButton>())
    {
        rad.Checked = false;
    }
    foreach (CheckBox chk in page.Controls.OfType<CheckBox>())
    {
        chk.Checked = false;
    }
}
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • 2
    Not a bad question, you simply did not understand and came to the right place to learn. +1 BTW @Heinzi, clearest explanation on the question. – Mr. Mr. Apr 25 '13 at 07:20
  • I would accept this answer as the edit is exactly what you want (the original code example wouldnt find any radio buttons :P) – Sayse Apr 25 '13 at 07:20
  • +1 for nice and clear answer. @Oliver, If you want to clear out all controls of a particular type from the form then [you may get them recursively](http://stackoverflow.com/a/3426721/961113) and then iterate through them using foreach. – Habib Apr 25 '13 at 07:24
5

Use the OfType<T>() method:

foreach (RadioButton item in yourChildern.OfType<RadioButton>())
{
    //your code
}
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
2

Use

quiztabs.TabPages.OfType<RadioButton>() and quiztabs.TabPages.OfType<CheckBox>() instead of quiztabs.TabPages in your code.

Alternativly;

foreach(Control c in this.Controls)
{
   if(c is RadioButton)
   {
     c.Checked = false;
   }
}

foreach(Control i in this.Controls)
{
   if(i is CheckBox)
   {
     i.Checked = false;
   }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Your problem is that .TabPages is a list of Tab Pages... I believe you want the controls on each tab page

foreach(TabPage t in quizTabs.TabPages)
{
       foreach (RadioButton rad in t.Controls)
        {
            rad.Checked = false;
        }
        foreach (CheckBox chk in t.Controls)
        {
            chk.Checked = false;
        }
}
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Note: you may wish to compare this with other answers about the OfType method (which i didnt know about) – Sayse Apr 25 '13 at 07:17
  • You would stil have that same cast error: `foreach RadioButton rad in t.Controls` doesn't filter, it casts everything (and fails) – Hans Kesting Apr 25 '13 at 07:20
  • Hence the note.. I maintain my comment in this answer still explains why OPs code isnt working – Sayse Apr 25 '13 at 07:22
0

The foreach needs to have an iterator (rad here) of the same type of whatever you want to iterate through (TabPages here):

So it would look something like:

foreach (TabPage tab in quiztabs.TabPages) {

Although TabPages would be whatever type of object that quiztabs.TabPages is.

Hope this makes sense!

WheretheresaWill
  • 5,882
  • 7
  • 30
  • 43
0

because, tabs in not a radioButton, you should do this instead,

foreach(TabPage tp in quizTabs.TabPages)
     {
        foreach (RadioButton rad in tp.Controls.OfType<RadioButton>())
        {
            rad.Checked = false;
        }
        foreach (CheckBox chk in tp.Controls.OfType<CheckBox>())
        {
            chk.Checked = false;
        }
    }
Pyromancer
  • 2,429
  • 5
  • 19
  • 28
0

Take one List<> of type RadioButton and all radiobutton in that list .

List<RadioButton> list = new List<RadioButton>();
            list.Add(radioButton1);
            list.Add(radioButton2);
            list.Add(radioButton3);
            foreach (RadioButton item in list)
            {
                if(tabControl1.SelectedTab==tabControl1.TabPages[tabControl1.SelectedIndex])
                item.Checked = false;
            }
Neelendu
  • 493
  • 1
  • 6
  • 12