how to use a lamda expression to find the radio button that is checked in a groupbox

I have a groupbox on a WinForm (groupBox1) that has radio buttons dynamically added to it.  I want to find the  first radio button that is checked when a button is clicked.  I tried using the following but it just returns null.

RadioButton rb1 = groupBox1.Controls.OfType<RadioButton>()
                           .FirstOrDefault(n => n.Checked);

Open in new window


I used the following code to make sure there is a RadioButton checked and this code works but I don't want to have to iterate over all the buttons.

foreach (RadioButton rb in groupBox1.Controls.OfType<RadioButton>())
{
    if (rb.Checked)
    {
        isChecked = true;
    }
    else
    {
        isChecked = false;
    }
}

Open in new window


When I debug the above code, I get a Radio Button that is checked as well as Radio Buttons that are not checked.

What do I have to do to get the lambda expression to return the first Radio Button that is checked?  Any help is greatly appreciated.
dyaroshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
There's absolutely nothing wrong with your LINQ-based attempt, so make sure that the radio buttons actually are members of the GroupBox, and that they are not on top of some other nested container like a Panel (that is inside the GB).

As far as your second attempt, just include a break on the true clause:

e.g.

foreach (RadioButton rb in groupBox1.Controls.OfType<RadioButton>())
{
    if (rb.Checked)
    {
        isChecked = true;
        break;
    }
    else
    {
        isChecked = false;
    }
}

Open in new window

0
dyaroshAuthor Commented:
When I debug the code and check the value of rb1 I don't get anything.

Lambda Expression Not Working
When I get to the Foreach statement it does find a RadioButton and I'm using the same syntax minus the lambda expression so the radio buttons are part of the GroupBox.

Foreach Working
Any other ideas?
0
käµfm³d 👽Commented:
In your first screenshot you need to step one more line (i.e. go to the if (rb.Checked) line) before you test the value in the Immediate Window.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

dyaroshAuthor Commented:
My first screen shot is showing that rb1 which is the variable I am trying to put the radio button in from the lambda expression doesn't show anything.  

The foreach statement is just showing that the radio buttons are in the GroupBox1 since that was one of the things I was told to confirm.

I only want the rb1 code and want to remove the foreach.  Ultimately I want my code to look as follows:

RadioButton rb1 = groupBox1.Controls.OfType<RadioButton>()
                           .FirstOrDefault(n => n.Checked);
switch (rb1.Text)
{
    case "Concordia":
        // do stuff here;
        break;
    case "Eye Med":
        // do stuff here
        break;
}

Open in new window

0
käµfm³d 👽Commented:
Well, like I said, there's nothing wrong with your lambda.
Untitled.png
Untitled.png
0
dyaroshAuthor Commented:
Do you think the problem is with how I am creating the Radio buttons as to why the LINQ expression isn't working?  Here is the code I use to create the radio buttons.

                // Set GroupBox size based on number of Carriers returned
                if (dt != null)
                {
                    groupBox1.Height = 40 + dt.Rows.Count * 20;
                    // Add Healthcare Carriers as options on form
                    int i = 1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        RadioButton rb = new RadioButton();
                        rb.Name = "rbCarrier" + i.ToString();
                        rb.Text = dr["CarrierName"].ToString();
                        rb.Location = new Point(7, 20 * i);
                        rb.Font = new Font("Microsoft Sans Serif", 10);
                        if (i == 1)
                            rb.Checked = true;
                        groupBox1.Controls.Add(rb);
                        i++;
                    }

                    // Add Export Button if Healthcare Carriers found
                    Button exportBtn = new Button();
                    exportBtn.Name = "btnExport";
                    exportBtn.Text = "Export Data";
                    exportBtn.Location = new Point(groupBox1.Location.X, groupBox1.Height + groupBox1.Location.Y);
                    exportBtn.Click += new System.EventHandler(this.btnExport_Clicked);
                    this.Controls.Add(exportBtn);
                }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dyaroshAuthor Commented:
I have recently returned to this project and now I can't recreate my original problem.  Thank you for the help and sorry it took me so long to get back to you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.