My decimal variables weren’t adding up using else if.
I have by now resolved all issues except for one.
code with explanation:
namespace Huiswerk5._2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
radioButton2.Checked = true;//only this doesnt work
}
private void button1_Click(object sender, EventArgs e)
{
decimal kleinekoffie = 0.50m;//here i make variables
decimal middelgrotekoffie = 0.75m;// of the decimal type
decimal grotekoffie = 1.00m;// i figured out to add the m at
decimal melk = 0.05m;//the end to be able to type numbers
decimal suiker = 0.05m;//with two decimals
decimal extrasterk = 0.25m;
decimal prijs = 0;
if (radioButton3.Checked == true)/// here are the if's
{
prijs = +kleinekoffie;
label3.Text = prijs.ToString("0.00");
if (checkBox3.Checked == true)
{
prijs = +kleinekoffie + melk;
label3.Text = prijs.ToString("0.00") + " Euro ";
}
if (checkBox2.Checked == true)
{
prijs = +kleinekoffie + suiker;
label3.Text = prijs.ToString("0.00") + " Euro";
}
if (checkBox1.Checked == true)
{
prijs = kleinekoffie + extrasterk;
label3.Text = prijs.ToString("0.00") + " Euro";
}
}
if (radioButton3.Checked == true)
if (checkBox3.Checked == true)
if (checkBox2.Checked == true)
if (checkBox1.Checked == false)
{
prijs = +kleinekoffie + melk + suiker;
label3.Text = prijs.ToString("0.00") + " Euro ";
}
if (radioButton3.Checked == true)
if (checkBox3.Checked == true)
if (checkBox1.Checked == true)
if (checkBox2.Checked == false)
{
prijs = +kleinekoffie + melk + extrasterk;
label3.Text = prijs.ToString("0.00") + " Euro ";
}
if (radioButton3.Checked == true)
if (checkBox2.Checked == true)
if (checkBox1.Checked == true)
if (checkBox3.Checked == false)
{
prijs = +kleinekoffie + suiker + extrasterk;
label3.Text = prijs.ToString("0.00") + " Euro ";
}
if (radioButton3.Checked == true)
if (checkBox2.Checked == true)
if (checkBox1.Checked == true)
if (checkBox3.Checked == true)
{
prijs = +kleinekoffie + melk + suiker + extrasterk;
label3.Text = prijs.ToString("0.00") + " Euro ";
}
if (radioButton2.Checked == true)
{
prijs = middelgrotekoffie;
label3.Text = prijs.ToString("0.00") + " Euro";
if (checkBox3.Checked == true)
{
prijs = middelgrotekoffie + melk;
label3.Text = prijs.ToString("0.00") + " Euro";
}
if (checkBox2.Checked == true)
{
prijs = middelgrotekoffie + suiker;
label3.Text = prijs.ToString("0.00") + " Euro";
}
if (checkBox1.Checked == true)
{
prijs = middelgrotekoffie + extrasterk;
label3.Text = prijs.ToString("0.00") + " Euro";
}
if (radioButton2.Checked == true)
if (checkBox3.Checked == true)
if (checkBox2.Checked == true)
if (checkBox1.Checked == false)
{
prijs = middelgrotekoffie + melk + suiker;
label3.Text = prijs.ToString("0.00") + "Euro";
}
if (radioButton2.Checked == true)
if (checkBox3.Checked == true)
if (checkBox2.Checked == true)
if (checkBox1.Checked == true)
{
prijs = middelgrotekoffie + melk + suiker + extrasterk;
label3.Text = prijs.ToString("0.00") + "Euro";
}
}
if (radioButton1.Checked == true)
{
prijs = grotekoffie;
label3.Text = prijs.ToString("0.00") + "Euro";
if (checkBox3.Checked == true)
{
prijs = grotekoffie + melk;
label3.Text = prijs.ToString("0.00") + "Euro";
}
if (checkBox2.Checked == true)
{
prijs = grotekoffie + suiker;
label3.Text = prijs.ToString("0.00") + "Euro";
}
if (checkBox1.Checked == true)
{
prijs = grotekoffie + extrasterk;
label3.Text = prijs.ToString("0.00") + "Euro";
}
}
if (radioButton1.Checked == true)
{
if (checkBox3.Checked == true)
if (checkBox2.Checked == true)
if (checkBox1.Checked == false)
{
prijs = grotekoffie + melk + suiker;
label3.Text = prijs.ToString("0.00") + "Euro";
}
}
if (radioButton1.Checked == true)
if (checkBox3.Checked == false)
if (checkBox2.Checked == true)
if (checkBox1.Checked == true)
{
prijs = grotekoffie + suiker + extrasterk;
label3.Text = prijs.ToString("0.00") + "Euro";
}
if (radioButton1.Checked == true)
if (checkBox3.Checked == true)
if (checkBox2.Checked == false)
if (checkBox1.Checked == true)
{
prijs = grotekoffie + melk + extrasterk;
label3.Text = prijs.ToString("0.00") + "Euro";
}
if (radioButton1.Checked == true)
{
if (checkBox3.Checked == true)
if (checkBox2.Checked == true)
if (checkBox1.Checked == true)
{
prijs = +grotekoffie + melk + suiker + extrasterk;
label3.Text = prijs.ToString("0.00") + "Euro";
}
}
}
}
}
i didnt understand that this sequence of "if’s" doesnt create "double reactions" so else if or return arent neccesary.
just make sure all possible combinations of radiobutton and checkbox are handles, including et to false.
the only thing that doesnt work, is making radiobutton2 autocheck itself at start up.
the code is at the very beginning, i got the code and where to write off the internet.. visual studio recognizes it as such, the only thing i can think of is that it needs to be at a different line.
any suggestions on this last mystery of this assignment?
greetings,
stefan.
2
Answers
The issue you seem to be encountering is that your if and else if statements are checking for the same condition. In this case, only the first statement will run as the second one is never checked since your first if statement has been successful. They would only both run when they are both if statements instead of an else if.
You will need to use a different condition in your else if statement for it to run as shown here with an example.
If you want the following line to run when checkBox3.Checked == True then I would recommend moving it inside the first if statement.
Hope this helps and good luck with your code.
First, make sure your indentation is correct. It will help you maintain your code more easily.
You code but indented right:
Now you probably see for yourself what is going on here. Please study it before reading further.
You check is
checkBox3.Checked
is trueIf not true, you check if it is true
and if it is, which will never ever be, you check
if
radioButton3.Checked
is trueDepending on what you need, the next could be a solution:
Although this code probably would work, it is wrong practice. The
Text
property is set twice, this is not neccessary and even unwanted.You have some values you should add together and then convert it to a string (if neccessary) and assign it to this
label3.Text
.(made a little edit here, you use decimals not floats, my mistake)
This is a good starting point to learn coding. There are way better methods but that’s for a later concern.
You can fancy up
label3.Text = price.ToString();
with:label3.Text = price.ToString("0.00");
This will output the price with only two decimals.
(edit: removed the parts of compilation error, it’s probably not there.
decimal + string
should work, my mistake)