我有下面的代码来使用for循环生成标签,但是for循环存在问题,如果productList有4个项目,它会生成1个标签而不是4个。解决问题是什么。C#动态生成标签
List<models.Car> carList = carController.getCars();
for (int i = 0; i < carList.Count; i++)
{
List<models.Product> productList = productController.getProducts(carList[i].Model);
for (int j = 0; j < productList.Count; j++)
{
productLabels.Add(new Label());
var productLabelsPoint = new System.Drawing.Point(200, 40 + i * 50);
(productLabels[j] as Label).Location = productLabelsPoint;
(productLabels[j] as Label).Size = new System.Drawing.Size(150, 15);
(productLabels[j] as Label).Text = productList[j].Title;
this.Tab.TabPages["tab1"].Controls.Add((productLabels[j] as Label));
}
}
哪个列表? carlist或productList? –
在问题中,当你说“如果列表中有4个项目”,“列表”是指productList还是carList? –
@LeonBambrick对于carList的每个增量,productList用新数据更新并生成一个新标签,直到carList完成。 – PRCube