3
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)

Daveo
  • 19,018
  • 10
  • 48
  • 71
Jeo Talavera
  • 527
  • 3
  • 8
  • 13
  • forgot to put the question.... private ArrayList label= new ArrayList(30); Score score = new Score(); Label label_class = new Label(); Random r = new Random(); – Jeo Talavera Aug 24 '11 at 05:05
  • Edit your question and put that code in it... And click the {} button to format your code correctly. – Dair Aug 24 '11 at 05:07

3 Answers3

4

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.

zellio
  • 31,308
  • 1
  • 42
  • 61
  • what is var?? anyways thanx it worked! now ill figure out how it worked! thanx mamen! – Jeo Talavera Aug 24 '11 at 05:26
  • hmmm how can i make the background color of all the labels to be color white? im trying to make a Pacman eater game without the AI ghosts – Jeo Talavera Aug 24 '11 at 05:28
  • [var](http://msdn.microsoft.com/en-us/library/bb383973.aspx) is a feature added in C# 3.0 that allows the compiler to determine the type of the variable for you rather than you specifying it. When types are obvious, as in this case, it helps make code readable and extendible. – zellio Aug 24 '11 at 05:28
  • Joe - See amended answer for White colouring – zellio Aug 24 '11 at 05:30
1

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.

Martin Doms
  • 8,598
  • 11
  • 43
  • 60
1

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

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71