0

I'm trying to add values of phones numbers based on checkbox's in the UI. For example if a checkbox1 (which represent phone1) is checked and checkbox2 is also checked then the program will add both phones' values. How can I add the value (in for loop for example) so that the if statement is lesser and simplified.

Here is my code:

public double totalPhone()
    {
        double total = 0;
        double item1 = 2249;
        double item2 = 1769;
        double item3 = 3099;
        double item4 = 1198;
        double item5 = 1899;

        if (chkPhone1.Checked == true)
        {
            total = total + item1;
        }

        if (chkPhone2.Checked == true)
        {
            total = total + item2;
        }

        if (chkPhone3.Checked == true)
        {
            total = total + item3;
        }

        if (chkPhone4.Checked == true)
        {
            total = total + item4;
        }

        if (chkPhone5.Checked == true)
        {
            total = total + item5;
        }

        return total;
    }
3
  • what kind of application is this? Commented Mar 31, 2016 at 2:31
  • Do you have fixed checkbox? Their values are fixed (like 2249, 1769, etc...) ? Commented Mar 31, 2016 at 2:36
  • What have you tried on the loop front? I'd imagine you would have something like the check boxes in a collection that you iterate through. Commented Mar 31, 2016 at 2:39

3 Answers 3

1

Assuming these checkbox's are all in the same GroupBox control just loop over the controls in that specific groupbox. I tested this and it seems to work. Use the checkbox item's Tag property to store the value associated to it:

public partial class Form1 : Form
{
    private static double Total { get; set; }

    private void Form1_Load(object sender, EventArgs e)
    {
        var ctrl = groupBox1;
        foreach (var checkBox in ctrl.Controls.OfType<CheckBox>())
        {
            Total = checkBox.Checked ? (Total + Convert.ToDouble(checkBox.Tag)) : Total;
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

How to fix this. "The name 'Total' does not exist in the current context"
Look at the variable Total in the class above
Why the Total keep doubling?
The Total variable in the code above is just summing all of he "checked" checkboxs
Thanks for helping me out!
0

I'm sure there is some re-factoring required on your code, but I don't see any case to use loop for this simple case.

Still interested? You could do something like this. Please note this code assume one to one mapping between item and checkbox name.

Dictionary<string, int> values = new Dictionary<string,int>();

int total = 0;
values.Add("item1", 2249);
values.Add("item2", 1769);
values.Add("item3", 3099);
values.Add("item4", 1198);
values.Add("item5", 1899);


foreach( CheckBox cb in this.Controls.OfType<CheckBox>()
                            .Where(c=>c.Checked))
{
    int itemprice;
    if(values.TryGetValue("item"+ Regex.Match(cb.Text, @"\d+").Value, out itemprice))
    {
         total+=itemprice;
    }
}

2 Comments

if I want to calculate the total price of the phone in certain groupbox, where should I change the code?
I get different value for different item. I don't know why.
0

You can store the ids of the checkboxes and its corresponding values in a Dictionary and then loop through the controls,check for its type and checked property and then add the value corresponding to the id of the checkbox from the dictionary.

Note:Code is not tested but this should get you on the way.

public double totalPhone()
{
    double total = 0;
    Dictionary<string,double> items = new Dictionary<string,double>();

    items.Add(chkPhone1.ID,2249); // ID,Text whatever works
    items.Add(chkPhone2.ID,1769);
    items.Add(chkPhone3.ID,3099);
    items.Add(chkPhone4.ID,1198);
    items.Add(chkPhone5.ID,1899);

    foreach(Control c in this.Controls)
    { 
        if(c is CheckBox && c.Checked)
        {
             total += (items[c.ID] != null ? items[c.ID] : 0);
        }
    }
    return total;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.