1

これには a を使用する必要がありますListBox

ListBox私は現在、このコードを使用してa から削除する方法を知っています:

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

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

1 に答える 1