newbie: Generating dynamic labels?

  • Thread starter Thread starter R
  • Start date Start date
R

R

Hi.

I need to generate a bunch (100) of lables to use in my application.

I'm sure there is a smarter way, than using the designer in VS...

Can some one fill in the missing part?
(having a private System.Windows.Forms.Label foo;)

[in constructor]
this.foo = new System.Windows.Forms.Label();

InitializeComponent();
for (int i=0;i<9;i++)
{
for (int j=1;j<=10;j++)
{
labelnumber = i*10+j;
this.foo.Location = new System.Drawing.Point(8+i*16, 8+(j-1)*12);
this.foo.Text = labelnumber.ToString();
this.foo.ForeColor = System.Drawing.Color.White;
this.pNumbers.Controls.Add(this.foo);
}
}

pNumbers is a panel.
Obviously I overwrite the already created labels. How can I avoid that? By
using an array? How?

Suggestions for solutions are highly appreciated!

Thanks in advance,

R

--
Due to heavy (20,000+/week!!) spam reception, I use a fake email.
Please reply in this group, which would help others as well.

Help fight spam:
NEVER reply to spam
NEVER buy anything from spamers
NEVER unsubscribe from a spam-list
 
You're adding the same label over and over again. Try this:

InitializeComponent()
{
for (int i=0;i<9;i++)
{
for (int j=1;j<=10;j++)
{
labelnumber = i*10+j;
Label label = new Label();
label.Location = new System.Drawing.Point(8+i*16, 8+(j-1)*12);
label.Text = labelnumber.ToString();
label.ForeColor = System.Drawing.Color.White;
this.pNumbers.Controls.Add(label);
}
}

}

The difference is, a new label is being created everytime. If you want
references to these labels, you should store them in some collection or
array.

This aside, do you really want to create so many labels? A label is a
control and it had a window associated with it. It's a bit of an overkill
to create so many windows... maybe there's another way to achieve what
you're trying to do. Could you please elaborate?

-vJ
 
You're adding the same label over and over again. Try this:

I know... I just couldn't figure out how to do this:
Label label = new Label();
If you want references to these labels, you should store them in some
collection or array.

of type Label (how? Label labels = new Label[]?)? Object?
This aside, do you really want to create so many labels? A label is a
control and it had a window associated with it. It's a bit of an overkill
to create so many windows... maybe there's another way to achieve what
you're trying to do. Could you please elaborate?

I'm pretty sure (but please enlight me, if I'm wrong), that I need a label
for each. I need to change content or color of each individual label later
in the process. Any suggestions to avoid all the tons of labels?

Thanks so far,

R
 
If you know the size of your collection you can use an array. From the
code, I assume you're laying out in a 2 dimensional area. So, your array
will be:

Label[,] labels;

In the InitializeComponent, you'll do:

labels = new Label[9,10];

for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 10; j++)
{
labels[i,j] = new Label();
labelNumber = i * 10 + j + 1;
...
...
}
}

Did you try using a listview control (in Report style)? You can actually
set the backcolors of the sub items.

-vJ


R said:
You're adding the same label over and over again. Try this:

I know... I just couldn't figure out how to do this:
Label label = new Label();
If you want references to these labels, you should store them in some
collection or array.

of type Label (how? Label labels = new Label[]?)? Object?
This aside, do you really want to create so many labels? A label is a
control and it had a window associated with it. It's a bit of an
overkill
to create so many windows... maybe there's another way to achieve what
you're trying to do. Could you please elaborate?

I'm pretty sure (but please enlight me, if I'm wrong), that I need a label
for each. I need to change content or color of each individual label later
in the process. Any suggestions to avoid all the tons of labels?

Thanks so far,

R
 
If you know the size of your collection you can use an array. From the
code, I assume you're laying out in a 2 dimensional area. So, your array
will be:
Did you try using a listview control (in Report style)? You can actually
set the backcolors of the sub items.

Thanks! Will take a closer look! (Havn't tried listview - yet...)

Thank you for taking your time to help newbies :)

Have a nice day!

R
 
R said:
Thanks! Will take a closer look! (Havn't tried listview - yet...)

Thank you for taking your time to help newbies :)

Have a nice day!

I would *strongly* suggest stopping GUI coding for a while, and
learning the basics of C#. This isn't meant to be an insulting
suggestion, but frankly writing a stable GUI is hard enough without
having to fight against an unfamiliar language too.

I suggest you start with some very simple console applications. Learn a
bit about collections, IO, maybe some threading, maybe database access
if your final app is going to need that, and *then* move onto GUI
stuff.
 
Back
Top