0

I have got the below code to generate labels using for loop, however there is a problem with the for loop, if the productList has 4 items, it generates 1 label instead of 4. I can't figure out what the problem is.

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));
    }
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
PRCube
  • 566
  • 2
  • 6
  • 19
  • which list? the carlist or the productList? – Leon Bambrick Apr 28 '15 at 05:24
  • In the question, when you say "if the list has 4 items" does 'the list' mean productList or carList ? – Leon Bambrick Apr 28 '15 at 05:27
  • @LeonBambrick for each increment of carList, productList is updated with new data and generates a new labels until the carList is finished. – PRCube Apr 28 '15 at 05:28
  • Ok @PRCube. I hear what you're saying. Now stick with me here. I have a question about your question. There is something that I cannot understand. I need to know the answer here, when you say "if *the list* has 4 items".... it's a good example... but I don't know what it means by 'the list'. – Leon Bambrick Apr 28 '15 at 05:30
  • @LeonBambrick I ment the productList, which is used as the number of labels to generate. so productList.Count = number of labels needed. – PRCube Apr 28 '15 at 05:33
  • Okay I updated my answer accordingly. Please see if it makes any change. – Leon Bambrick Apr 28 '15 at 05:38
  • @LeonBambrick unfortunately, it only generates label for the last two items in the productList. I think it might be overlapping like you said. – PRCube Apr 28 '15 at 05:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76411/discussion-between-leon-bambrick-and-prcube). – Leon Bambrick Apr 28 '15 at 06:40

1 Answers1

4

This only relies on i, not on j:

System.Drawing.Point productLabelsPoint = new System.Drawing.Point(200, 40 + i * 50);

So you might be drawing the labels one on top of the other. In which case, you'd need to add j into the mix, for example like this:

System.Drawing.Point productLabelsPoint = new System.Drawing.Point(200, 40 + i * 50 + j * 50);

I would also change the way you are referencing the label. (I can't tell from the context if what you are doing is okay or not, as it depends how that productLabels variables has been instantiated.)

PRCube
  • 566
  • 2
  • 6
  • 19
Leon Bambrick
  • 26,009
  • 9
  • 51
  • 75
  • just above my code, this is how I declare the labels. ArrayList productLabels = new ArrayList(); – PRCube Apr 28 '15 at 05:45
  • 1
    Rather add a new variable telling the number of the label added and use that to calculate the position, since they might still overlap. And the referencing the label from the List was definitely wrong. – Sami Kuhmonen Apr 28 '15 at 05:45