我正在尝试使用List<>中的项目填充列表框C#

时间:2013-12-09 21:16:11

标签: c# list listbox

我正在尝试使用列表中的项目填充列表框<>并能够以不同的形式称呼它。但它似乎没有加载到另一种形式。谢谢你的帮助。

private void frmAdminMenu_Load(object sender, EventArgs e)
    {
        Product prod1 = new Product(001, "Milk", "Dairy", 1.20m, 10, 1.027m);
        Product prod2 = new Product(002, "Cheese", "Dairy", 2.80m, 20, 0.300m);
        Product prod3 = new Product(003, "Apple", "Fruit", 0.50m, 10, 0.136m);
        Product prod4 = new Product(004, "Orange", "Fruit", 0.80m, 20, 0.145m);
        Product prod5 = new Product(005, "Tomato", "Veg", 2.50m, 15, 0.110m);
        Product prod6 = new Product(006, "Onion", "Veg", 1.50m, 10, 0.105m);
        Product prod7 = new Product(007, "Lamb", "Meat", 4.50m, 10, 0.340m);
        Product prod8 = new Product(008, "Chicken", "Meat", 3.50m, 10, 0.907m);
        products.Add(prod1);
        products.Add(prod2);
        products.Add(prod3);
        products.Add(prod4);
        products.Add(prod5);
        products.Add(prod6);
        products.Add(prod7);
        products.Add(prod8);


       FillProductListBox();
    }

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

尝试以另一种形式调用列表框

ListBox tmpProducts;

    public frmEnterShop()
    {
        InitializeComponent();
    }
    List<Product> shoppingCart = new List<Product>();

    public frmEnterShop(ListBox shippedIn)
        : this()
    {
        tmpProducts = shippedIn;
        MessageBox.Show("Total of " + tmpProducts.Items.Count, "Number of Items"); // view-Output
    }

 private void frmEnterShop_Load(object sender, EventArgs e)
    {

       lstViewProducts.Items.Add(tmpProducts.Items[0]);
       lstViewProducts.Items.Add(tmpProducts.Items[1]);
       lstViewProducts.Items.Add(tmpProducts.Items[2]);
       lstViewProducts.Items.Add(tmpProducts.Items[3]);
       lstViewProducts.Items.Add(tmpProducts.Items[4]);
       lstViewProducts.Items.Add(tmpProducts.Items[5]);
       lstViewProducts.Items.Add(tmpProducts.Items[6]);
       lstViewProducts.Items.Add(tmpProducts.Items[7]);
    }

1 个答案:

答案 0 :(得分:1)

我用

做了一个例子
listbox.DataSource = something;

注意ToString-Method的实现,以便在列表框中显示。

这是我的测试代码:

namespace WindowsFormsApplication1
{

    public class Product
    {
        String name;

        public Product(String name)
        {
            this.name = name;
        }

        public override string ToString()
        {
            return name;
        }
    }

    public partial class Form1 : Form
    {
        IList<Product> list = new List<Product>() { new Product("abc"), new Product("def") };

        public Form1()
        {
            InitializeComponent();
            listBox1.DataSource = list;
        }

    }
}

还有一个提示:请勿对产品ID使用所谓的幻数。为此使用增量变量:

int id = 0;
Product prod1 = new Product(++id, "Milk", "Dairy", 1.20m, 10, 1.027m);
Product prod2 = new Product(++id, "Cheese", "Dairy", 2.80m, 20, 0.300m);
Product prod3 = new Product(++id, "Apple", "Fruit", 0.50m, 10, 0.136m);
Product prod4 = new Product(++id, "Orange", "Fruit", 0.80m, 20, 0.145m);
Product prod5 = new Product(++id, "Tomato", "Veg", 2.50m, 15, 0.110m);
Product prod6 = new Product(++id, "Onion", "Veg", 1.50m, 10, 0.105m);
Product prod7 = new Product(++id, "Lamb", "Meat", 4.50m, 10, 0.340m);
Product prod8 = new Product(++id, "Chicken", "Meat", 3.50m, 10, 0.907m);