我的任务是创建一个包含2个列表框和3个标签的表单。
一个列表框里装满了露营装备的集合。当通过双击选中此列表框中的项时,它会将其复制到第二个列表框中。此处的一个列表将填充放置在第二个列表框中的每个项目的小计。
我被告知我应该使用并行数组,这样在最后,如果需要从第二个列表框中删除项,它也会从最后的小计中减去它。这个应用程序的关键是在与列表框平行的内存中拥有一个列表对象。
例如,您需要一个价目表来匹配可用的产品名称列表。您将需要相同的购买项目列表和所选项目的购买价格列表。通过执行此操作,您可以根据位置轻松地从订单中删除项目。
如果用户想要删除items purchased列表框中的item 3,您还需要从内存中的items purchase list对象中删除item 3。然后,您可以调用一个循环遍历采购价目表对象的方法,以获取总数。
这是我的代码,到目前为止,我知道我已经很接近了,但我似乎找不到缺失的部分:
private void lstItems_DoubleClick(object sender, EventArgs e)
{
////lstItems.DataSource = lstprices;
// validate item is selected
// get price of selected item and add to order list
decimal SubTotalValue = 0;
int itemIndex = 0;
decimal ThisPrice = 0;
{
try
{
if (lstItems.Items.Count > 0)
{
lstOrder.Items.Add(lstItems.SelectedItem.ToString());
itemIndex = int.Parse(lstItems.GetItemText(lstItem.ToString()));
ThisPrice = lstprices.IndexOf(itemIndex);
SubTotalValue += ThisPrice;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
////lstItems.SelectedIndex = lstprices.SelectedIndex;
// get string value of selected item and add to order listbox
lblSubtotal.Text = lstprices.ToString();
decimal STotal = decimal.Parse(lblSubtotal.Text.ToString());
decimal SubTotals = STotal * (tax / 100);
decimal GTotal = calcGrandTotal(STotal, SubTotals);
//display totals from calculation performing functions
lblTax.Text = SubTotals.ToString();
lblTotal.Text = GTotal.ToString();
}
}我会假设我会做一些类似的事情来删除项目。任何帮助都将不胜感激。以下代码在以下行出现错误:
lstOrder.Items.Add(lstItems.SelectedItem.ToString());
itemIndex = int.Parse(lstItems.GetItemText(lstItem.ToString())); 我不知道该怎么解决它
发布于 2018-10-25 08:37:47
我认为,如果用户可以在'lstItems‘中选择多个项目,则此代码必须进行如下更改
for(int i = 0 ; i < lstItems.SelectedItems.Count ; i++)
{
lstOrder.Items.Add(lstItems.SelectedItems[i].ToString());
int itemIndex = int.Parse(lstItems.GetItemText(lstItems.SelectedItems[i]);
}https://stackoverflow.com/questions/52979342
复制相似问题