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);
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>())
isChecked = true;
isChecked = false;
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.