-
Dynamic Labels again!!
hi:
I've created one label array in one form like this:
//first in the beginning of the code,
private System.Windows.Forms.Label mylabel[];
//then inside the button event
this.mylabel = new System.Windows.Forms.Label[1];
this.mylabel[0].Location = new System.Drawing.Point(200, 100);
this.mylabel[0].Size = new System.Drawing.Size(50, 50);
this.mylabel[0].TabIndex = 0;
Controls.AddRange( new System.Windows.Forms.Control[] {this.mylabel[0]});
no problem when compile it, but it got crashed when click the button, what is the problem?
THankx
-
What line did it crash at ?
If you have Visual Studio, the debugger should show you, if not, put a MessageBox before each line and see which one you get last.
You might want to have a closer look at this line
private System.Windows.Forms.Label mylabel[];
Put the brackets after the type and before the name, unlike C++.
-
> this.mylabel = new System.Windows.Forms.Label[1];
...does not create a single label. It creates an array of references to labels but does not initialise any of the elements.
mylabel[0] = new System.Windows.Forms.Label();
-
it crashed always at the first either one of lines:
this.mylabel[0].Location = new System.Drawing.Point(200, 100);
this.mylabel[0].Size = new System.Drawing.Size(50, 50);
this.mylabel[0].TabIndex = 0;
don't know why :(