簡體   English   中英

如何單擊一個按鈕,該按鈕將從ListBox中刪除一個項目,而該列表中包含C#中ListBox的信息?

[英]How do I click a button that will remove an item from a ListBox and the List that holds the information for the ListBox in C#?

我必須為此使用一個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刪除了。 我在想,我需要做的是從列表中刪除選定的行,然后清除ListBox並用更新的List數據重新填充它。 但我正在努力尋找方法。

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類:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM