Jeo Talavera
Jeo Talavera

Reputation: 537

How can I create labels inside a For loop

private ArrayList label= new ArrayList(30);

Label label_class = new Label();
Random r = new Random();

for (int i = 0; i < label.Count; i++) {
    ((Label)label[i]).Location = new Point(r.Next(ClientRectangle.Right -10),
                                           r.Next(ClientRectangle.Bottom - 10));
    ((Label)label[i]).Text = "o";
    ((Label)label[i]).Click += new EventHandler(Form1_Load);

    this.Controls.Add((Label)label[i]);

    ((Label)label[i]).Show();
}

This for loop is inside the Form1_Load so that it will run when the form is loading.. the problem is that, when I break point, I see that the code inside the forloop is not noticed by the break point/does not run. why is that?? and how can i create many labels randomly placed on form1(window form)

Upvotes: 3

Views: 11141

Answers (3)

zellio
zellio

Reputation: 32484

The issue is in

private ArrayList label= new ArrayList(30);

This creates an ArrayList of size 30, not one with 30 elements.

If you do something like

List<Label> labels = new List<Label>();

for (int i = 0; i < 30; i++) {
    var temp = new Label();

    temp.Location = new Point(r.Next(ClientRectangle.Right -10),
                                       r.Next(ClientRectangle.Bottom - 10));
    temp.Text = "o";
    temp.Click += new EventHandler(Form1_Load);

    temp.BackColor = System.Drawing.Color.White;

    this.Controls.Add(temp);

    temp.Show();
    labels.Add(temp) 
}

It should work.

In addition, notice my use of List<Label> instead of ArrayList there are cases where you will want to have the ability to specify the objects you are taking out but generally ( and in this case ) the generic forms where you specify the type will sure you much better. You will not need to do all of the boxing that you are doing and you will write fewer lines of code all of which will be more readable.

Upvotes: 4

Adam Tuliper
Adam Tuliper

Reputation: 30152

There's really two questions here :

  1. why are your breakpoints not being hit
  2. will this code work.

First, change your code instead of using an ArrayList use

private List<Label> label= new List<Label>();

and you can stop the ugly casting all over the place. You need to change label.Count to be 30 instead as well.

Secondly, breakpoints not being hit is almost always a sign of your debug information being out of sync from your code or the debugger could not find your PDB (debug info) file.

Ensure you are using a debug build. Check out the following: breakpoints in code behind not hit Also refer to my blog to check if symbols are loaded in the section on telling VS to load your PDB file (if the clean, etc doesn't work on your solution but it should!)

http://completedevelopment.blogspot.com/2009/11/debugging-in-gac-all-ways-to-accomplish.html

Upvotes: 1

Martin Doms
Martin Doms

Reputation: 8748

The line

private ArrayList label= new ArrayList(30);

does not create an ArrayList with 30 items. It constructs an ArrayList with an initial capacity of 30, but still no items (see documentation here). The for loop is not running because label.Count is zero.

Consider changing it to something like

private ArrayList label = new ArrayList();
for (int i = 0; i < 30; i++) label.Add(new Label(...));

Followed by the rest of your code.

Upvotes: 1

Related Questions