468,539 Members | 1,906 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

How to create an array of label

jhs
Hello,

I developping a .NET windows form application an need some help to create an
array of System.Windows.Forms.Label in order to be able to manage all of
them using index.
I'm trying to do this in form_load void:
System.Windows.Forms.Label[] labelArray = new
System.Windows.Forms.Label[91];

for (i=1;i<91;i++)

{

labelArray[i].Size = new System.Drawing.Size(33, 33);

labelArray[i].Location = new System.Drawing.Point(i, i);

labelArray[i].Text=i.ToString();

this.Controls.AddRange(new System.Windows.Forms.Control[] {labelArray[i]});
But at running time i get a 'System.NullReferenceException'

How do i have to create my array in order to avoid to create all labels at
design time.

Thanks in advance
Nov 16 '05 #1
3 31045

you have to intialize to new label in each loop, becoz the array values will
be null.

System.Windows.Forms.Label[] labelArray = new
System.Windows.Forms.Label[91];

for (i=1;i<91;i++)
{

//intialize new label
labelArray[i] = new Label();

labelArray[i].Size = new System.Drawing.Size(33, 33);

labelArray[i].Location = new System.Drawing.Point(i, i);

labelArray[i].Text=i.ToString();

this.Controls.AddRange(new System.Windows.Forms.Control[] {labelArray
[i]});
}

Shak
"jhs" <jo*********@bull.net> wrote in message
news:#A**************@tk2msftngp13.phx.gbl...
Hello,

I developping a .NET windows form application an need some help to create an array of System.Windows.Forms.Label in order to be able to manage all of
them using index.
I'm trying to do this in form_load void:
System.Windows.Forms.Label[] labelArray = new
System.Windows.Forms.Label[91];

for (i=1;i<91;i++)

{

labelArray[i].Size = new System.Drawing.Size(33, 33);

labelArray[i].Location = new System.Drawing.Point(i, i);

labelArray[i].Text=i.ToString();

this.Controls.AddRange(new System.Windows.Forms.Control[] {labelArray[i]});

But at running time i get a 'System.NullReferenceException'

How do i have to create my array in order to avoid to create all labels at
design time.

Thanks in advance

Nov 16 '05 #2
You need to add the following to your loop to create a new instance of a
label:
labelArray[i] = new Label();
You may also have an issue with all of the labels being on top of each
other. I'm not sure what you are looking to accomplish. To space them out
more, you could do something like:

labelArray[i].Location = new System.Drawing.Point(i*30, i*30);
"jhs" <jo*********@bull.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello,

I developping a .NET windows form application an need some help to create an array of System.Windows.Forms.Label in order to be able to manage all of
them using index.
I'm trying to do this in form_load void:
System.Windows.Forms.Label[] labelArray = new
System.Windows.Forms.Label[91];

for (i=1;i<91;i++)

{

labelArray[i].Size = new System.Drawing.Size(33, 33);

labelArray[i].Location = new System.Drawing.Point(i, i);

labelArray[i].Text=i.ToString();

this.Controls.AddRange(new System.Windows.Forms.Control[] {labelArray[i]});

But at running time i get a 'System.NullReferenceException'

How do i have to create my array in order to avoid to create all labels at
design time.

Thanks in advance

Nov 16 '05 #3
* "jhs" <jo*********@bull.net> scripsit:
I developping a .NET windows form application an need some help to create an
array of System.Windows.Forms.Label in order to be able to manage all of
them using index.
I'm trying to do this in form_load void:
System.Windows.Forms.Label[] labelArray = new
System.Windows.Forms.Label[91];

for (i=1;i<91;i++)

{
\\\
labelArray[i] = new Label();
///
labelArray[i].Size = new System.Drawing.Size(33, 33);

labelArray[i].Location = new System.Drawing.Point(i, i);

labelArray[i].Text=i.ToString();

this.Controls.AddRange(new System.Windows.Forms.Control[] {labelArray[i]});


Replace the line above with:

\\\
this.Controls.Add(labelArray[i]);
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Francois | last post: by
3 posts views Thread by John Haigh | last post: by
12 posts views Thread by =?Utf-8?B?RGFuaWVs?= | last post: by
reply views Thread by davaahuu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.