gadreel
gadreel

Reputation: 3

C# How to calculate the total sum of a selection of a Listbox

I encounter a problem on my code, I explain to you what I wish to obtain:

  1. I have a listbox with a dictionary, the dictionary allows me to change the choices of my listbox into more complex values

Code of my dictionnary :

class RacesMask
{
    Dictionary<string, string> rMask = new Dictionary<string, string>();

    public RacesMask()
    {
        rMask.Add("Toutes", "-1");
        rMask.Add("Humains", "1");
        rMask.Add("Orcs", "2");
        rMask.Add("Nains", "3");
        rMask.Add("Elfes de la nuit", "4");
        rMask.Add("Mort-vivants", "5");
        rMask.Add("Taurens", "6");
        rMask.Add("Gnomes", "7");
        rMask.Add("Trolls", "8");
        rMask.Add("Gobelins", "9");
        rMask.Add("Elfes de Sang", "10");
        rMask.Add("Draeneis", "11");
        rMask.Add("Worgens", "22");
        rMask.Add("Pandarens", "24");
        rMask.Add("Sacrenuit", "27");
        rMask.Add("Tauren de Haut-Roc", "28");
        rMask.Add("Elfe du vide", "29");
        rMask.Add("Draeneï sancteforge", "30");
    }

    public Dictionary<string, string> GetRacesMask() { return rMask; }

}
  1. Now, I would like that when I select the various values ​​of my Listbox, it does a calculation, for example:

Select: Human (1 in my dictionaries) and Orc (2 in my dictionnary)

and a messagebox shows me the result 2 + 1 = 3

  1. But, actually my listbox only calculates one selected value and not the others.

Code of my listbox :

private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
    {
        RacesMask raceMask = new RacesMask();
        Dictionary<string, string> rM = raceMask.GetRacesMask();

        foreach (var rMask in listBox1.SelectedItems)
        {

            var selection1 = int.Parse(rM[rMask.ToString()]);
            var selection2 = int.Parse(rM[rMask.ToString()]);
            var total = selection1 + selection2;

            MessageBox.Show(total.ToString());

        }

Picture of the current result

Thank you in advance for your help

Upvotes: 0

Views: 975

Answers (3)

Jon
Jon

Reputation: 579

You're resetting total with each iteration of the loop. Either declare your total variable outside your loop and add to it with each iteration, or you could use LINQ:

private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
    var raceMask = new RacesMask();
    var rM = raceMask.GetRacesMask();

    var total = (from s in listBox1.SelectedItems
                 select int.Parse(rM[s.ToString()]).Sum();

    MessageBox.Show(total);
}

Upvotes: 1

Frank Ball
Frank Ball

Reputation: 1126

You logic is flawed. It will only return a single selection's value. Try this:

private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
    RacesMask raceMask = new RacesMask();
    Dictionary<string, string> rM = raceMask.GetRacesMask();
     int total = 0;
    foreach (var rMask in listBox1.SelectedItems)
    {
        total += int.Parse(rM[rMask.ToString()]);
    }
    MessageBox.Show(total.ToString());

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

You can have a variable total outside the loop and then sum the value of each selected item in the ListBox in the total vairable like :

int total = 0;
foreach (var rMask in listBox1.SelectedItems)
{

    selection = int.Parse(rM[rMask.ToString()]);
    total = total + selection;

}

MessageBox.Show(total.ToString()); // now show the result finally.

Upvotes: 1

Related Questions