'Is it possible to have multiple checkboxes in the same 'if' statement?

I'm very new to coding and am currently learning C#. More specifically I'm currently struggling with using checkboxes in group boxes. What I'm wondering is if it's possible to have multiple checkboxes in the same 'if' statement?

What I mean by is that, instead of just (for example):

private void button1_Click(object sender, Eventargs e)
{
  float optionACheckBox = 149.00f;

  if (checkBox1.Checkstate == CheckState.Checked)
  {
     finalPrice = (optionACheckBox + 60)
  }
  MessageBox.Show(finalPrice);
}

how would I make it so that instead it would be something like:

if (checkBox1 & checkBox2 = CheckState.Checked)
{
 ((Value of checkBox1) + (Value of checkBox2) + 60)
}

where if the user checks checkBox1 & checkBox2 then both values can be used in the equation?

Regarding this, would it also be possible to make code that checks if any/what combination of the CheckBoxes in the GroupBox have been checked? Rather than me writing an 'if' statement for every single combination of the different CheckBoxes?

Thank you to anyone who takes the time to read this and for any tips given

More Context (Sorry this is my second ever StackOverFlow post so I'm not very good at proper formatting):

  • Using Visual Studio 2019

  • There are three CheckBoxes in total, and all three are in one GroupBox

  • The CheckBoxes and GroupBox were added to the form using the toolbox feature



Solution 1:[1]

You didn't really tell us much about your logic but it seems like you want a hypothetical scenario like:

  • some fixed base price of the pizza
  • a price for each topping
  • a fixed price for applying any number of toppings

As such I'd look at accumulation:

float price = 100;

if(anchoviesCheckbox.Checked)
  basePrice += 20;

if(olivesCheckbox.Checked)
  basePrice += 30;

if(chilliesCheckbox.Checked)
  basePrice += 40;

//if anchovies AND chillies are chosen there is a surcharge because the most of the chefs are allergic to this combination and we have to get the manager to do it
if(anchoviesCheckbox.Checked && chilliesCheckBox.Checked)
  basePrice += 50;

//if any one OR more topping has been applied, there is a surcharge
if(anchoviesCheckbox.Checked || olivesCheckbox.Checked || chilliesCheckbox.Checked)
  basePrice += 60;

These accumulate because there is no else - if the checkbox is checked, cost is added to the final price. Each checkbox is viewed in isolation. Later some also act in combination.

Note you could of course also check if the base price is greater than 100 to know if to apply a surcharge..


Once you've got that clear in your mind how it works, you can save yourself some of those ifs by treating the checkboxes in the group box as a collection

You can do this in the form constructor:

//put the prices in the checkbox Tag:

anchoviesCheckbox.Tag = 20;

olivesCheckbox.Tag = 30;

chilliesCheckbox.Tag = 20;

Then in your price calc method you can do something like:

float basePrice = 100:

foreach(var cb in toppingsGroupBox.Controls.OfType<CheckBox>().Where(cb => cb.Checked))
  basePrice += (int)cb.Tag;

This uses LINQ to pull out just the checked checkboxes, and then extract the price from their tag and add it on. You could even do:

float basePrice = 100 +
  toppingsGroupBox.Controls
    .OfType<CheckBox>()
    .Where(cb => cb.Checked)
    .Sum(cb => (int)cb.Tag);

For your adding a surcharge if any topping is chosen:

if(toppingsGroupBox.Controls
    .OfType<CheckBox>()
    .Any(cb => cb.Checked)))
basePrice += 60;

You could now add 100 different toppings checkboxes to the form and so long as you put the price in the tag there is no more code needs adding to the method that calculates the price

The "manager must add anchovies and chillies" surcharge could also be done with LINQ. For that you'd have to define a custom set of checkboxes and if they are all ticked then apply the surcharge:

class PizzaForm:Form{

  CheckBox[] managerSurcharge;

  PizzaForm() { // constructor

    ...
    managerSurcharge = new[]{ anchoviesCheckBox, chilliesCheckBox };

  }

Then in the price:

if(managerSurcharge.All(cb => cb.Checked))
  basePrice += 50;

Not an if in sight! ? (yeah, it's really hard to do away with every if..)

But do name your controls and variables well; this code here is easily readable and obvious what it does because of the names. That manager surcharge scenario would be a lot more confusing if you write:

CheckBox array1 = new[]{ checkbox12, checkbox7 }

Solution 2:[2]

You can use switch case judgments to assign different outcomes to these different choices.

But because every combination is a judgment situation, no matter what judgment method you use, you need to judge them separately.

The judgment to judge that both are selected should be as follows:

 if (checkBox1.Checked && checkBox2.Checked)
            {
                //To do something
            }

Some documents: checked ?&&?||?== and !=

Solution 3:[3]

First of all, you can't compare the checkbox itself with a CheckState so you have to use checkBox1.CheckState == CheckState.Checked or better using Boolean "Checked" property: if(checkBox1.Checked) which will check if the checkbox is checked.

As for your last Question on how to detect if any checkbox is checked you can use || the logical "OR" Operator:

if(checkBox1.Checked || checkBox2.Checked || checkBox3.Checked)

Update

Logical Operator vs Conditional Short Circuit Operator

  1. Logical Operator: & is used to ensure all Operands are evaluated and checked.
  2. Conditional Short Circuit Operator: && is used to skip the right side Operand.

As mentioned in the comments, both will work in the vast majority of the cases, but I would always use the "Conditional Short Circuit Operators" when possible, to skip the unnecessary Check of the right side Operand.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3