Variable in a label name?

  • Thread starter Thread starter Tinus
  • Start date Start date
T

Tinus

Hello,

I want to do the following:

I have 10 labels on my form, called Label0 to Label9. Now I want to change
the Text property of all the labels at once, e.g.:

for (int i = 0; i < 10; i++)
{
Label(i).Text = "Hello, this is label: "+i.ToString();
}

Now, ofcourse this won't work. But how can this be done?

Thanks for your help,
Tinus
 
How do you put the labels on your form? I would handle this differently
depending on whether you were creating them through the designer or in code.
 
How about adding your Labels to an array in your ctor after InitializeComponent then you can write almost exactly that code

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hello,

I want to do the following:

I have 10 labels on my form, called Label0 to Label9. Now I want to change
the Text property of all the labels at once, e.g.:

for (int i = 0; i < 10; i++)
{
Label(i).Text = "Hello, this is label: "+i.ToString();
}

Now, ofcourse this won't work. But how can this be done?

Thanks for your help,
Tinus
 
Tinus,

You mean something as this
\\
foreach ( Control ctr in this.Controls)
{
if (ctr is Label)
if (ctr.Name.Substring(0,5) == "label")
ctr.Name = ctr.Name.Substring(5);
}
///

I hope this helps?

Cor
 
Your iteration will not work in C#, only in Visual Basic
This code will work

private void SetLabelNames() {
int counter=0;
foreach(Control ctrl in this.Controls){
Type typ = ctrl.GetType();
if(typ.Name !="Label"){
string nm = ctrl.Text;
int len = nm.Length();
nm.Insert(0,"Hello, this is label ");
nm.Insert(len,counter.ToString());
counter ++;
}
}
}
 
I don;t really see the point in having such inefficient code when you already know how many labels there are.

class MyForm : Form
{
Label[] labels = new Label[10];
public MyForm()
{
InitializeComponent();
labels[0] = ...
...
labels[9] = ...
}
private void SetTheLabels()
{
for(int i = 0; i < labels.Length; i++)
{
labels.Text = "Hello, this is label: "+i;
}
}
}

No checking controls that don't matter, no unnecessary type checking and coercion. Very clean readable code.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Your iteration will not work in C#, only in Visual Basic
This code will work

private void SetLabelNames() {
int counter=0;
foreach(Control ctrl in this.Controls){
Type typ = ctrl.GetType();
if(typ.Name !="Label"){
string nm = ctrl.Text;
int len = nm.Length();
nm.Insert(0,"Hello, this is label ");
nm.Insert(len,counter.ToString());
counter ++;
}
}
}


--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.7.1 - Release Date: 19/01/2005



[microsoft.public.dotnet.languages.csharp]
 
Richard Blewett said:
How about adding your Labels to an array in your ctor after
InitializeComponent then you can write almost exactly that code

Alternatively, don't use the designer at all, and don't end up with 10
different variables to start with - just put them straight in an array
:)
 
Richard,

You are a develop mentor you write.

This was the question,
\\\
I have 10 labels on my form, called Label0 to Label9. Now I want to change
the Text property of all the labels at once, e.g.:
///
Your comment on other aswer was this
I don;t really see the point in having such inefficient code when you
already know how many labels there >are.

Your code does in my opinion not fit the question and is useless long.

When you really comment code from other people, do it than right by
instance.
\\\
Label[] labels = {label0, label1,etc};
for (int i= 0;i > 2;i++)
labels.Name=i.ToString();
///

Just my thougth

Cor
 
Cor Ligthert said:
You are a develop mentor you write.

This was the question,
\\\
I have 10 labels on my form, called Label0 to Label9. Now I want to change
the Text property of all the labels at once, e.g.:
///
Your comment on other aswer was this


Your code does in my opinion not fit the question and is useless long.

It certainly *does* fit the question. It makes it easy and efficient to
get to a specific label. Personally, as I wrote elsewhere, I'd just
create the labels in a loop to start with, but then I'm not a fan of
the GUI designer...

The code that John posted is somewhat broken - it doesn't check that
the full name uses the right namespace, for instance. It's also
inefficient in two ways - it calls GetType rather than using the "is"
operator, and it goes through *all* the controls on the form. Richard's
code is much cleaner, IMO. John's code also wouldn't compile, as Length
is a property, and the two Insert calls wouldn't do anything useful as
the return values are ignored.

<snip>
 
Jon,
It certainly *does* fit the question. It makes it easy and efficient to
get to a specific label. Personally, as I wrote elsewhere, I'd just
create the labels in a loop to start with, but then I'm not a fan of
the GUI designer...

Tinus had already long before that told that it was my sample that did fit.
So this was not only about Johns code however as well mine. Where I tried to
show a more standard approach for handling this kind of questions. You would
know that I often in the "general" group take the approach I showed in the
message in this thread.
It certainly *does* fit the question. It makes it easy and efficient to
get to a specific label. Personally, as I wrote elsewhere, I'd just
create the labels in a loop to start with, but then I'm not a fan of
the GUI designer...
The question was about renaming labels created with the designer. That you
are not a fan of that has nothing to do with that. This are in my opinion
two separated questions than, in addition this question was not in the
message from the OP.

Cor
 
Cor Ligthert said:
Tinus had already long before that told that it was my sample that did fit.

He then responded to Richard with: "Thanks, this one is even better."
though. (When you say "long before" - it was half an hour before
Richard posted his answer. Not really that long, was it?)
So this was not only about Johns code however as well mine. Where I tried to
show a more standard approach for handling this kind of questions. You would
know that I often in the "general" group take the approach I showed in the
message in this thread.

And I still think that in general using an array for this kind of thing
is much better than iterating through controls.
The question was about renaming labels created with the designer. That you
are not a fan of that has nothing to do with that. This are in my opinion
two separated questions than, in addition this question was not in the
message from the OP.

The OP's question was about finding a good way to access certain
controls programmatically. Using an array is *definitely* the best way
to go here, regardless of whether or not the controls are created in
the designer in the first place. Arrays are made for the kind of
indexing that Tinus was after.
 
Jon,

I did not check it, however I was probably in the language.vb newsgroup the
first who was using fixed arrays for this problem already a long time ago.
It took some time before it was taken over.

The only problem is with showing that directly is, that it is not showing
to somebody who is new that there is as well an itegrated collection of
controls in every control.

Cor
 
Cor Ligthert said:
I did not check it, however I was probably in the language.vb newsgroup the
first who was using fixed arrays for this problem already a long time ago.
It took some time before it was taken over.

Well, the first post in this newsgroup on this thread was only made an
hour before Richard's post...
The only problem is with showing that directly is, that it is not showing
to somebody who is new that there is as well an itegrated collection of
controls in every control.

But why show an inferior solution? There are any number of things that
neither solution shows, but what's important (IMO) is whether or not
the solution solves the problem in the best way - and that's what
Richard's solution did, IMO.
 
Jon,

When you have this opinion than why not ask Microsoft to delete the designer
from C#, instead of telling too people in this newsgroup that they should
not use it.

However, I don't agree with you, the designer is a very usefull instrument
that should be used where it is productive.

Cor
 
Jon,

However I am glad that you now like that fixed array, because I thought to
remember me that you was with the ones who critisezed me because there
should be a hashtable be used for that.

Cor
 
Cor Ligthert said:
When you have this opinion than why not ask Microsoft to delete the designer
from C#, instead of telling too people in this newsgroup that they should
not use it.

Because it's a matter of opinion, and while I don't find it useful for
production code, I know that others do.
However, I don't agree with you, the designer is a very usefull instrument
that should be used where it is productive.

It's okay for prototyping, but you tend to end up with code which is
hard to maintain and read.
 
Cor Ligthert said:
However I am glad that you now like that fixed array, because I thought to
remember me that you was with the ones who critisezed me because there
should be a hashtable be used for that.

In the same situation, where the OP specifically wanted to index by an
integer? It sounds unlikely, but I'm sure you'll be happy to prove me
wrong. Which thread was it?
 
Back
Top