100 Labels

D

David de Passos

Hi!
I have 100 Labels and I need to put in text property a value but I don't
want to write 100 lines of code, like something like that:

for i=1 to 100
xrLabel(i).Text= i
next

How can do that?

--


Cumprimentos,
David de Passos
--------------------------------------------------------------
RCSOFT, Lda.
E-Mail: (e-mail address removed)
Móvel: +351 966931639
Telefone: +351 239708708
Fax: +351 239708701
Tel. Directo: +351 239708705 ext. 401
 
J

Jon Skeet [C# MVP]

David de Passos said:
I have 100 Labels and I need to put in text property a value but I don't
want to write 100 lines of code, like something like that:

for i=1 to 100
xrLabel(i).Text= i
next

How can do that?

Create an array of labels instead. You won't be able to do it in the
designer, but it's trivial to do in code.
 
N

Neil Mosafi

Well there's no standard way so it depends on a few things.

If you wanna name all the labels on your form you can iterate through
the controls collection and if the control's type is Label then act
accordingly e.g
foreach (Control control in this.Controls) {
if (control is Label) ((Label) control).Text = "text";
}

If you need to be more specific, you could create an array containing
the labels you are interested in, and then iterate through them eg
public Label[] GetLabels() {
return new Label[] { this.SomeLabel, this.AntherLabel, ...}
}

Or if you have a consistent naming strategy you could use:
for (int i = 0; i < count; i++) {
((Label) this.FindControl (string.Format("label{0}", i))).Text =
i.ToString();
}

Regards
Neil
 
J

Jon Skeet [C# MVP]

Neil Mosafi said:
Well there's no standard way so it depends on a few things.

If you wanna name all the labels on your form you can iterate through
the controls collection and if the control's type is Label then act
accordingly e.g
foreach (Control control in this.Controls) {
if (control is Label) ((Label) control).Text = "text";
}

If you need to be more specific, you could create an array containing
the labels you are interested in, and then iterate through them eg
public Label[] GetLabels() {
return new Label[] { this.SomeLabel, this.AntherLabel, ...}
}

Or if you have a consistent naming strategy you could use:
for (int i = 0; i < count; i++) {
((Label) this.FindControl (string.Format("label{0}", i))).Text =
i.ToString();
}

Note that unless you really want to use the designer for the labels,
you can just have a member which has the labels in, and they needn't be
referenced by any other variable. This is a variant on your second
solution, but one which doesn't require you to set each label up
manually in the designer to start with, and doesn't require you to
build a new array each time.
 
G

Guest

Create a Custom Label Control , Which reads the the value to be displayed
from an XML file.The XML file can have a id,value pair schema.

This way you need not code the values to be displayed.Also you can change
the values without compiling the application.

To increase perfomance you can cache the XML file.

Pandu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Key pressed 3
Autosize + Datagrid 1
100 Labels 3
IP 1
Running Program 8
TextBox 2
DataGrid 2 1
Query Timeout 3

Top