Anyone know how to approarch this
Say, for a certain program (specifically in the button click), I require atleast 1 out of the 3 radio buttons to be checked in order to move to the next form, how would I approach this, because C# doesn't have the and/or statement like VB does :(, or maybe it does but I just haven't figured it out yet.
I tried doing an if approach where if the radiobutton.checked == false, you would get a message telling you to check the radio button or something. This works fine if I only had 1 radio button, but I'm using 3.
And if I were to do an else if approach for each of the radio buttons, I would always get a message saying the radio button isn't clicked and whatever, because i can only click one button at a time.
So any ideas on how to approach this
Sorry if this sounded confusing, I'll try to clear it up if anyone has some problems understanding!

Multiple Radiobuttons with if statement?
mfareezaw
another one - in regards to the first example:
if (this.theRadioButton1.Checked || this.theRadioButton2.Checked || this.theRadioButton3.Checked)
{
//continue - one of them is checked
}
houtexwebdev
What you can do is that if you have radio buttons on the form, you can either go through each of them manually (if else) or go through the controls on the form and see if the control is a RadioButton, if so, check the value - but it may take a while to go through the controls on the form if you have a large amount. I'll provide both examples:
//manually:
bool isARadioButtonChecked = false;
if (this.theRadioButton1.Checked == true)
{
isARadioButtonChecked = true;
}
else if (this.theRadioButton2.Checked == true)
{
isARadioButtonChecked = true;
}
else if (this.theRadioButton3.Checked == true)
{
isARadioButtonChecked = true;
}
if (isARadioButtonChecked == false)
{
MessageBox.Show("You must select an option");
}
else
{
//continue
}
//Example2: go through each control and check the value of a radio button
bool isARadioButtonChecked = false;
foreach(Control currentControl in this.Controls)
{
if (currentControl.GetType() == typeof(RadioButton)
{
RadioButton currentRadioButton = ((RadioButton)currentControl);
if (currentRadioButton.Checked == true)
{
isARadioButtonChecked = true;
}
}
}
if (isARadioButtonChecked == false)
{
MessageBox.Show("You must select an option");
}
else
{
//continue
}
does this help
DavidC#2005
If you have only Fixed number of Radio Buttons Say 3 then you need to write this code:
if(!radioButton1.Cehcked && !radioButton2.Cehcked && !radioButton3.Cehcked)
{
//No Radio button Checked
}
See the Behavior of Radio buttons, it lets a user to select atkeast 1 out of several options, when you check a radion button out of many checked state of all radio button in the same parent control is toggled. So I mean to say if you dont want to use any validation and make sure all things are fine, by default set one's checked property of of 3 to true at design time.
Cheers ;-)