473,322 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 32413

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Francois | last post by:
Is it possible to automatically create an array on load page? I want my script to be external. (<script language="JavaScript" src="somescript.js"></script>) Several pages should use this...
3
by: John Haigh | last post by:
I have the need to create an array of objects. Now this sound fairly trivial but I can't figure this out. I have one class called PostingObjectService that has a method GetPostings where the...
0
by: Raju | last post by:
Hi all i am Rajendran I am working as a asp.net programmer. I am frish to this field so please any of u garify my doubt I Create array of Textbox Dynamically and not passible to retrieve data...
1
by: dragannis | last post by:
Hello, I realy need help about this, I have database that I want to use in my Vb code, and I need to create array from elements of one field from that mdb. Why : I need to do some calculations...
12
by: =?Utf-8?B?RGFuaWVs?= | last post by:
Hi, I have create a class name employee. Next, i would like to create array for the employee class. The code as below: dim eply(5) as employee when i call eply(0).name="Khrish", an error...
6
by: Zytan | last post by:
I have an enum. I want a struct of data associated with each one. Then, I could use the enum to access the data as needed. In other words, I want the enum to represent more than just a unique...
6
by: coldude | last post by:
hi there, I need to be able to generate multiple labels for the use in a Windows form, the quantity of labels would depend on a count, is it possible to generate multiple labels in code? If so how...
2
by: codecln | last post by:
How do i dynamically create a label in html using ajax ? please help me out in this basic issues...
4
by: ingaivey | last post by:
I have 81 text boxes on a form and would like to create a text array in code that will equate to these boxes on the form. Once I've assigned each text box from the form to the array entry, I'd just...
0
by: davaahuu | last post by:
how to create array with audio files?, Is it possible?
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.