我想填充列表框中的項目列表<>並能夠以不同的形式調用它。但似乎並沒有加載到其他表格上。謝謝你的幫助。我想填充列表框中的項目從列表<> 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]);
}
*旁註*:你在'frmEnterShop_Load'中做的事情應該在'for'循環中。 –