jeudi 25 mars 2021

How to loop through check boxes and assign enumeration values when boxes are checked?

I have a group of check boxes, to be precise there are 3 boxes. It works for me when using if statement but I wonder there is a way to loop through check boxes and assign enumeration values when a box is checked or more.

The code looks like this:

if (chkTomato.Checked && !chkLettuce.Checked && !chkCarrot.Checked)
{
    cart.VegChosen = Veggies.Tomato;
}
else if (!chkTomato.Checked && chkLecctuce.Checked && !chkCarrot.Checked)
{
    cart.VegChosen = Veggies.Lecctuce;
}
else if (!chkTomato.Checked && !chkLecctuce.Checked && chkCarrot.Checked)
{
    cart.VegChosen = Veggies.Carrot;
}
else if (chkTomato.Checked && chkLettuce.Checked && chkCarrot.Checked)
{
    cart.VegChosen = Veggies.All;
}
else if (chkTomato.Checked && chkLettuce.Checked && !chkCarrot.Checked)
{
    cart.VegChosen = Veggies.TomatoAndLettuce;
}
else if (chkTomato.Checked && !chkLettuce.Checked && chkCarrot.Checked)
{
    cart.VegChosen = Veggies.TomatoAndCarrot;
}
else if (!chkTomato.Checked && chkLettuce.Checked && chkCarrot.Checked)
{
    cart.VegChosen = Veggies.LettuceAndCarrot;
}
else
{
    cart.VegChosen = Veggies.None;
}

I want to find out a way to loop it in case there are more than just 3 check boxes, the if statement would be very long.

Thank you!




Aucun commentaire:

Enregistrer un commentaire