中其他类的列表中添加和删除项目

中其他类的列表中添加和删除项目

本文介绍了在C#中其他类的列表中添加和删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用表格在C#中设置基本的购物车.我已经将一些产品加载到List<>中,打开时可以显示,但是我的问题是,当我从List<>中添加或删除项目并退出菜单并再次打开商店时,会显示原始列表.我不确定该如何在原始列表中显示的商店中添加或删除它?感谢您的帮助.

I am setting up a basic shopping cart in C# using forms. I have loaded a few product into the List<> that can be shown when opened up, but my problem is that when I add or remove an item from the List<> and exit to menu and open shop again the original list is shown. I not sure how to add or remove it from shop that it show up in the original list as well? Thanks for any help.

public partial class frmMain : Form
{
    private List<Product> products = null;

    public List<Product> GetStock()
    {
        List<Product> products = new List<Product>();

        products.Add(new Product("D001", "Milk", "Dairy", 1.20m, 10, 1.027m));
        products.Add(new Product("D002", "Cheese", "Dairy", 2.80m, 20, 0.300m));
        products.Add(new Product("F001", "Apple", "Fruit", 0.50m, 10, 0.136m));
        products.Add(new Product("F002", "Orange", "Fruit", 0.80m, 20, 0.145m));
        products.Add(new Product("V001", "Tomato", "Veg", 2.50m, 15, 0.110m));
        products.Add(new Product("V002", "Onion", "Veg", 1.50m, 10, 0.105m));
        products.Add(new Product("M001", "Lamb", "Meat", 4.50m, 10, 0.340m));
        products.Add(new Product("M002", "Chicken", "Meat", 3.50m, 10, 0.907m));

        return products;
    }

}

public partial class frmAdminMenu : Form
{

    Product products = new Product();
    List<Product> tmpProducts = (new frmMain()).GetStock();


    private void btnAdminAddStock_Click(object sender, EventArgs e)
    {
        Product tmp = new Product(txtCode.Text, txtDescription.Text, category, Convert.ToDecimal(txtPrice.Text),
            Convert.ToInt16(txtQuantity.Text), Convert.ToDecimal(txtWeight.Text));
        if (tmp != null)
            tmpProducts.Add(tmp);

        loadProducts();

    }

    private void frmAdminMenu_Load(object sender, EventArgs e)
    {
        string heading = "Code:\tDescription:\tCategory:\tPrice:\tStock:\tWeight:\n";
        lstViewStock.Text = heading.ToString();

        loadProducts();
    }

    private void loadProducts()
    {
        lstViewStock.Items.Clear();
        foreach (Product p in tmpProducts)
        {
            lstViewStock.Items.Add(p.GetDisplayText("\t"));
        }
    }

    private void btnAdminRemoveStock_Click(object sender, EventArgs e)
    {
        tmpProducts.RemoveAt(lstViewStock.SelectedIndex);

        //tmpProducts.RemoveAt(lstViewStock.SelectedItems);
        loadProducts();
    }


}

}

推荐答案

这样做时,

List<Product> tmpProducts = (new frmMain()).GetStock();

您正在将列表重新初始化回frmMainGetStock返回的原始列表.因此,当您打开新的frmAdminMenu

You are reinitializing the list back to the original list returned by GetStock in frmMain. That's the reason it shows you the same list when you open up a new frmAdminMenu.

您应该尝试将列表保留为model对象的一部分,并在关闭表单时将其保存(保存到数据库,文件系统,内存),以便保存更改,否则,表单实际上并没有做任何东西.

You should try keeping the list as part of a model object, and saving it (to the database, file system, memory) when you close the form so the changes are saved, otherwise, the form is not really doing anything.

这篇关于在C#中其他类的列表中添加和删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:42