0

I'm trying to iterate through a GroupBox of GroupBoxes of RadioButtons, but I'm getting stuck because the GroupBoxes also contain other nodes such as Labels and TextBoxes. For better clarification, the tree of nodes is:

Groupbox
\/
GroupBox(es) + TextBox + Label
\/
RadioButton(s) + TextBox

My goal is to iterate through all possible RadioButtons, finding which in each section is checked, if any (or in other words, has the property (radiobuttonname).Checked == True) I can't seem to do this, though, as there's a problem with every solution I've come up with.

The Code:

int i = 0;
RadioButton[] currentData = [null, null, null, null, null, null, null, null];
        
foreach(Control group in check.Controls)
{
    if (group is GroupBox)
    {
        foreach(Control rb in group.Controls)
        {
            if (rb is RadioButton) //All RadioButtons are correctly recognized (confirmed by print test)
            {
                if ((RadioButton) rb.Checked){ //Cannot Resolve Symbol "Checked"
                    currentData[i] = ((RadioButton) rb);
                    break;
                }
            }
        }
                
        i++;
    }
}

return currentData;

The above should first enter the primary GroupBox, then enter each secondary GroupBox, then iterate through each RadioButton and add the checked RadioButton to the to-be-returned array (leaving null if there is none).

Solutions/Issues: -I could run these as foreach(RadioButton rb in group.Controls)? But if I do, the program fails because I'm casting a TextBox as a Radiobutton -I could run foreach as a for loop instead, finding a way to bypass the issue? But the number of nodes in each GroupBox varies, and I don't believe I can iterate through one like an array -I could cast the Controls (that I know are RadioButtons) as RadioButtons, as seen above? But as long at they become Controls first, they seem to lose the ability to access the .Checked property -I could remove all non-RadioButtons from the GroupBoxes? But that would defeat the point of the application I'm making...

Any help would be appreciated. I've been stuck on this for days, and would like to move on.

2
  • Use if(rb is RadioButton rb2), then rb2.Checked. Commented Mar 21, 2024 at 12:55
  • It is a matter of precedence. if (((RadioButton)rb).Checked){ ... should work (note the added parentheses around the cast). Commented Mar 21, 2024 at 13:28

2 Answers 2

0

As much as you call something something specific, does not make it so. Your rb variable is a Control Type. However, if you where to cast it to a RadioButton and say that it should act like it and not just have the name thereoff, then it would act as you would like it to. There is more than one way to do it, but you could do something like this:

foreach(Control cntrl in group.Controls)
    {
        if (cntrl is RadioButton rb) //All RadioButtons are correctly recognized (confirmed by print test)
        {
            //do stuff with you RadioButton rb
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

huh, you can just Do That with "is"? Worked like a charm, thanks!
0

It seems the issue is in this condition

if ((RadioButton) rb.**Checked**)

In this, I have replaced ** Checked ** with Checked. Moreover, I have initialized the radio button array with 8 elements and changed rb to a RadioButton inside the if condition. try this code and let me know

int i = 0;
RadioButton[] currentData = new RadioButton[8];
        
foreach(Control group in check.Controls)
{
    if (group is GroupBox groupBox)
    {
        foreach(Control control in groupBox.Controls)
        {
            if (control is RadioButton radioButton && radioButton.Checked)
            {
                currentData[i] = radioButton;
                break; 
            }
        }
                
        i++;
    }
}

return currentData;

1 Comment

Ah, the ** Checked ** was a typo from trying to bold that word, oops. Fixed that in the post! Like the answer above, this worked great! Thanks much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.