Oliver
Oliver

Reputation: 831

foreach c# gives unable to cast error

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.

Upvotes: 3

Views: 1541

Answers (7)

Pyromancer
Pyromancer

Reputation: 2509

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

Upvotes: 0

Neelendu
Neelendu

Reputation: 513

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

Upvotes: 0

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98868

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

Upvotes: 2

Heinzi
Heinzi

Reputation: 172468

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

Upvotes: 20

WheretheresaWill
WheretheresaWill

Reputation: 6500

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!

Upvotes: 0

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32521

Use the OfType<T>() method:

foreach (RadioButton item in yourChildern.OfType<RadioButton>())
{
    //your code
}

Upvotes: 5

Sayse
Sayse

Reputation: 43330

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

Upvotes: 0

Related Questions