validation question
hey everyone, i'm new programming wondering knew how catch error if radio buttons not selected. i'm making program displays amount of calories needed maintain current weight.
i tried a.. if(maleradiobutton.checked=false) display message, doesn't seem work. sorry if code, have never posted on here. code straight forward appreciate.
public partial class form1 : form{
const double male_active_factor = 15;
const int male_inactive_factor = 13;
const int female_active_factor = 12;
const int female_inactive_factor = 10;
public form1()
{
initializecomponent();
}
private void calculatebutton_click(object sender, eventargs e)
{
double calories;
double weightinpounds;
double weightinkilograms;
if (maleradiobutton.checked && activeradiobutton.checked)
{
double.tryparse(weightpoundstextbox.text, out weightinpounds);
double.tryparse(weightkgtextbox.text, out weightinkilograms);
weightinkilograms = weightinpounds * 0.45;
calories = weightinkilograms * male_active_factor;
calorieoutputlabel.text = calories.tostring();
}
if (maleradiobutton.checked && inactiveradiobutton.checked)
{
double.tryparse(weightpoundstextbox.text, out weightinpounds);
double.tryparse(weightkgtextbox.text, out weightinkilograms);
weightinkilograms = weightinpounds * 0.45;
calories = weightinkilograms * male_inactive_factor;
calorieoutputlabel.text = calories.tostring();
}
if (femaleradiobutton.checked && activeradiobutton.checked)
{
double.tryparse(weightpoundstextbox.text, out weightinpounds);
double.tryparse(weightkgtextbox.text, out weightinkilograms);
weightinkilograms = weightinpounds * 0.45;
calories = weightinkilograms * female_active_factor;
calorieoutputlabel.text = calories.tostring();
}
if (femaleradiobutton.checked && inactiveradiobutton.checked)
{
double.tryparse(weightpoundstextbox.text, out weightinpounds);
double.tryparse(weightkgtextbox.text, out weightinkilograms);
weightinkilograms = weightinpounds * 0.45;
calories = weightinkilograms * female_inactive_factor;
calorieoutputlabel.text = calories.tostring();
}
// following used check if first name , last name has text in it.
string okmessage = "information correct.";
string errormessage = "";
maleradiobutton.checked = true;
if (firstnametextbox.text == "")
{
errormessage = "first name text box empty. ";
}
if (lastnametextbox.text == "")
{
errormessage = errormessage + "last name text box empty.";
}
if (firstnametextbox.text == "" || lastnametextbox.text == "")
{
messagebox.show(errormessage, "error message");
}
else
{
messagebox.show(okmessage);
}
}
you have places in code test if radiobutton checked
if (maleradiobutton.checked && ...
if want test if radiobutton not checked use not operator !
if (!maleradiobutton.checked) // not male
you said tried
if (maleradiobutton.checked = false)
but didn't work. should have got warning compiler code , green squiggly line under it. not ignore warnings. warning be
assignment in conditional expression constant, did mean use == instead of = ?
the warning tells how fix code.
paul linton
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment