im extremely new to c# so im having a little difficulty with the following problem.
below is my following code
int access = 1;
int onlineVideos = 2;
int personalTrainer = 20;
int dietConsultation = 20;
int extras = 0;
if (checkBox1.Checked)
{
extras += access;
}
else if (checkBox2.Checked)
{
extras += onlineVideos;
}
else if (checkBox3.Checked)
{
extras += personalTrainer;
}
else if (checkBox4.Checked)
{
extras += dietConsultation;
}
textBox6.Text = extras.ToString();
i want to be able to easily add the sums of all the boxes that are checked, and then print them to a textbox.
According to your description, you don't need multiple if/else clauses, but only a method which sums all values of your check boxes whose state is checked. Something like this:
foreach (Control c in this.Controls)
{
if((c is CheckBox) && ((CheckBox) c).Checked)
{
yourdecimal += Convert.ToDecimal(c.Text);
}
}
The exact method depends on your exact code, so it might be required to change the "this.Controls" as example. Or it might be necessary to add try/catch to prevent issues if there are non numeric values etc. This is just a way to do this. If you need further assistance, you should please add your source code.