如何单击一个按钮,从ListBox和C#中保存ListBox信息的List中删除一个项目

本文关键字:一个 ListBox 删除 项目 保存 信息 List 何单击 单击 按钮 | 更新日期: 2023-09-27 18:24:08

我必须为此使用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并用更新的列表数据重新填充它。但我正在努力想办法做到这一点。

表格1:

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

购物篮类别:

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)
    {
        ????
    }
}

订单项类别:

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

如何单击一个按钮,从ListBox和C#中保存ListBox信息的List中删除一个项目

您的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 } );