I'm making a windows form in C# where a user must select more than one RadioButton, however only 1 RadioButton can be selected.
I provide an image to illustrate my problem

and this is my code:
if (radioButton3.Checked == true)
{
if (radioButton4.Checked == true)
{
NpgsqlCommand cmd10 = new NpgsqlCommand("INSERT INTO visual_acuity (eid, corrected, is_left_eye, value) VALUES (@eid, @corrected, @is_left_eye, @value)", conn);
cmd10.Parameters.AddWithValue("@eid", textBox1.Text);
cmd10.Parameters.AddWithValue("@value", textBox17.Text);
cmd10.Parameters.AddWithValue("@is_left_eye", radioButton2.Checked);
cmd10.Parameters.AddWithValue("@corrected", radioButton4.Checked);
cmd10.ExecuteNonQuery();
}
if (radioButton5.Checked == true)
{
NpgsqlCommand cmd11 = new NpgsqlCommand("INSERT INTO visual_acuity (eid, corrected, is_left_eye, value) VALUES (@eid, @corrected, @is_left_eye, @value)", conn);
cmd11.Parameters.AddWithValue("@eid", textBox1.Text);
cmd11.Parameters.AddWithValue("@value", textBox17.Text);
cmd11.Parameters.AddWithValue("@is_left_eye", radioButton2.Checked);
cmd11.Parameters.AddWithValue("@corrected", radioButton4.Checked);
cmd11.ExecuteNonQuery();
}
}
else
{
if (radioButton4.Checked == true)
{
NpgsqlCommand cmd10 = new NpgsqlCommand("INSERT INTO visual_acuity (eid, corrected, is_left_eye, value) VALUES (@eid, @corrected, @is_left_eye, @value)", conn);
cmd10.Parameters.AddWithValue("@eid", textBox1.Text);
cmd10.Parameters.AddWithValue("@value", textBox17.Text);
cmd10.Parameters.AddWithValue("@is_left_eye", radioButton2.Checked);
cmd10.Parameters.AddWithValue("@corrected", radioButton4.Checked);
cmd10.ExecuteNonQuery();
}
if (radioButton5.Checked == true)
{
NpgsqlCommand cmd11 = new NpgsqlCommand("INSERT INTO visual_acuity (eid, corrected, is_left_eye, value) VALUES (@eid, @corrected, @is_left_eye, @value)", conn);
cmd11.Parameters.AddWithValue("@eid", textBox1.Text);
cmd11.Parameters.AddWithValue("@value", textBox17.Text);
cmd11.Parameters.AddWithValue("@is_left_eye", radioButton2.Checked);
cmd11.Parameters.AddWithValue("@corrected", radioButton4.Checked);
cmd11.ExecuteNonQuery();
}
}
If you have any suggestions, please feel free to comment. it would be a big help. thanks!
Radio buttons got their name from the station buttons on a car radio, where only one station can be selected at a time. And that is the purpose of radio button controls.
Only one button in a group can be selected. Also consider that experienced users instantly know only one option can be selected just by looking at them because they behave consistently across applications. So changing the way radio buttons work would be confusing to users.
If you want more than one selected at a time, then you do not want a radio button. Use checkboxes instead.
You have to put RadioButtons into Panels or GroupBoxes.
Then you can select them independently in each such group.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With