如何单击将从ListBox中删除项目的按钮以及包含C#中ListBox信息的List?

时间:2013-07-22 11:42:26

标签: c# winforms list class collections

我必须使用ListBox

我目前知道如何使用此代码从ListBox中删除:

private void removeButton_Click(object sender, EventArgs e)
{
    REMOVE();
}

private void REMOVE()
{
    int c = lstCart.Items.Count - 1;

    for (int i = c; i >= 0; i--)
    {
        if (lstCart.GetSelected(i))
        {
            lstCart.Items.RemoveAt(i);
        }
    }
}

但我发现,当我添加另一个项目时,它会清除ListBox并显示列表中的每个项目,因此它包含“已删除”项目,因为它未从列表中删除,只有ListBox。 我想我需要做的是从列表中删除选定的行,然后清除ListBox并使用更新的列表数据重新填充它。但我很难找到如何做到这一点。

Form1中:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void addButton_Click(object sender, EventArgs e)
    {
        OrderItem currentItem = new OrderItem(txtItemName.Text,
        Decimal.Parse(txtPrice.Text), (int)numQTY.Value);
        myBasket.Add(currentItem);
        lstCart.Items.Clear();
        foreach (OrderItem i in myBasket)
        {
            lstCart.Items.Add(String.Format("{0,-40}  
            {1,-40}  {2,-40}  {3,-40}", i.ProductName, 
            i.Quantity.ToString(), i.LatestPrice.ToString(),
            i.TotalOrder.ToString()));
        }
        txtItemTotal.Text = "£" + myBasket.BasketTotal.ToString();
        txtItemCount.Text = myBasket.Count.ToString();
    }

    private void removeButton_Click(object sender, EventArgs e)
    {
        int c = lstCart.Items.Count - 1;

        for (int i = c; i >= 0; i--)
        {
            if (lstCart.GetSelected(i))
            {
                lstCart.Items.RemoveAt(i);
            }
        }

        ????
    }
}

ShoppingBasket class:

public class ShoppingBasket
{
    public ShoppingBasket()
    {

    }

    public new void Add(OrderItem i)
    {
        base.Add(i);
        calcBasketTotal();
    }

    private void calcBasketTotal()
    {
        BasketTotal = 0.0M;
        foreach (OrderItem i in this)
        {
            BasketTotal += i.TotalOrder;
        }
    }

    public new void Remove(OrderItem i)
    {
        ????
    }
}

OrderItem类:

public class OrderItem
{
    public OrderItem(string productName, 
    decimal latestPrice, int quantity)
    {
        ProductName = productName;
        LatestPrice = latestPrice;
        Quantity = quantity;
        TotalOrder = latestPrice * quantity;
    }

    public string ProductName { get; set; }

    public decimal LatestPrice { get; set; }

    public int Quantity { get; set; }

    public decimal TotalOrder { get; set; }
}

1 个答案:

答案 0 :(得分:1)

您的add方法在基类上调用Add()。你有你的基类删除?如果是这样,请使用它。否则,请将ShoppingBasket课程更改为:

Orders列表会存储您的OrderItems。添加和删​​除此列表。

public class ShoppingBasket
{
    public List<OrderItem> Orders { get; set; }

    public ShoppingBasket()
    {
        Orders = new List<OrderItem>();
    }

    public void Add(OrderItem orderItem)
    {
        Orders.Add(orderItem);
    }


    public void Remove(OrderItem orderItem)
    {
        Orders.Remove(orderItem);
    }
}

用法:

var basket = new ShoppingBasket();
basket.Add( new OrderItem { OrderItemId = 1, ... } );

// Remove an OrderItem
basket.Remove( new OrderItem { OrderItemId = 1 } );