简体   繁体   中英

listboxes passing values from each other

I'm trying to multiply the values of 2 listboxes together and make their product appear at another list box I'm getting the results I need but the problem is when I rerun the loop using a command button the listbox removes the next instance of the first value calculated by ppc[i] * qty[i] but when I try to remove the the listBox4.Items.Remove(ppc[i] * qty[i]) it reprints the whole array again from first element to last element

string myString = textBox1.Text.ToString();
        int index = listBox6.FindString(myString, -1);
        int[] qty = new int[99];
        int[] ppc = new int[99];
        int[] gt1 = new int[99];

        listBox3.Items.Add(listBox5.Items[index]);
        listBox1.Items.Add(textBox2.Text.ToString());
        if (index != -1)
        {
            listBox6.SetSelected(index, true);
            listBox2.Items.Add(textBox1.Text); //name
        }

        listBox3.Items.Add(listBox5.Items[index]);
        listBox3.Items.Remove(listBox5.Items[index]);

        for (int i = 0; i != listBox2.Items.Count ; i++)
        {
            ppc[i] = Convert.ToInt32(listBox3.Items[i]);
            qty[i] = Convert.ToInt32(listBox1.Items[i]);
            listBox4.Items.Remove(ppc[i] * qty[i]);
            listBox4.Items.Add((ppc[i] * qty[i]));
        }

My understanding is that this loop works once, and then when it is re-run it is out of order. Are you making sure to clear listbox4 each time this loop is executed? Also since listBox2 isn't used, it is probably better not to use it for your loop bounds.

if(listBox1.Items.Count == listBox3.Items.Count)
{

     int rowCount = listBox1.Items.Count;
     listBox4.Items.Clear();

     for (int i=0; i < rowCount; i++)
     {
            ppc[i] = Convert.ToInt32(listBox3.Items[i]);
            qty[i] = Convert.ToInt32(listBox1.Items[i]);
            listBox4.Items.Insert(i , (ppc[i] * qty[i]));
     } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM