2013-12-09 38 views
0

我想填充列表框中的項目列表<>並能夠以不同的形式調用它。但似乎並沒有加載到其他表格上。謝謝你的幫助。我想填充列表框中的項目從列表<> c#

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]); 
    } 
+0

*旁註*:你在'frmEnterShop_Load'中做的事情應該在'for'循環中。 –

回答

1

我做了使用

listbox.DataSource = something; 

通知了toString-法在列表框中顯示的具體實現。

這裏是我的測試代碼:

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); 
+0

這是一個偉大的提示,謝謝 – user3058734

+0

不要忘記標記爲答案,如果我幫你;) – rulebot

+0

我試着添加'lstViewProducts.DataSource =產品;'但它並不適用於我 – user3058734