• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

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.
0
dyarosh
Asked:
dyarosh
  • 4
  • 3
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now