100 Labels

  • Thread starter Thread starter David de Passos
  • Start date Start date
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
 
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.
 
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
 
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.
 
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
 
Back
Top