简体   繁体   English

彼此传递值的列表框

[英]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 我正在尝试将2个列表框的值相乘,并使它们的乘积出现在另一个列表框中。我得到了所需的结果,但是问题是当我使用命令按钮重新运行循环时,列表​​框删除了下一个实例由ppc [i] * qty [i]计算的第一个值,但是当我尝试删除listBox4.Items.Remove(ppc [i] * qty [i])时,它将重新打印整个数组,从第一个元素到最后一个元素

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? 您确定每次执行此循环时都清除listbox4吗? Also since listBox2 isn't used, it is probably better not to use it for your loop bounds. 另外,由于未使用listBox2,所以最好不要将其用于循环边界。

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]));
     } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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