我试图用列表中的项目填充列表框<&燃气轮机;c#
我试图用列表中的项目填充列表框,并能够以不同的形式调用它。但它似乎没有加载到另一种形式。谢谢你的帮助我试图用列表中的项目填充列表框<&燃气轮机;c#,c#,list,listbox,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", "Dair
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]);
}
listboxtmp产品;
公共财政部
{
初始化组件();
}
List shoppingCart=新列表();
公共船厂(列表箱船厂)
:此()
{
tmpProducts=shippedIn;
MessageBox.Show(“总计”+tmpProducts.Items.Count,“项目数”);//查看输出
}
私有void frmEnterShop_加载(对象发送方,事件参数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]);
}
我用
listbox.DataSource = something;
请注意用于在listbox中显示的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;
}
}
}
旁注:您在
frmEnterShop_Load
中所做的操作应该是在for
循环中进行的。这是一个很好的提示,谢谢您,如果我帮助了您,请不要忘了标记为答案;)我尝试添加'lstViewProducts.DataSource=products;'但是它对meCan不起作用,你可以发布你目前正在编写的类和产品类吗?这会有很大帮助。顺便说一句:你在编程什么样的项目?ASP.NET?WinForms?WPF?银灯?
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);