469,338 Members | 8,284 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

Variable in a label name?

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
Nov 16 '05 #1
18 22516
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.

"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in message
news:ee***************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #2
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

Nov 16 '05 #3
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
Nov 16 '05 #4
Thanks Cor,

That did it for me!

Tinus

"Cor Ligthert" <no************@planet.nl> wrote in message
news:et****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #5
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 ++;
}
}
}

Nov 16 '05 #6
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[i].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/<11*********************@f14g2000cwb.googlegroups. com>

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]
Nov 16 '05 #7
Richard Blewett [DevelopMentor] <ri******@NOSPAMdevelop.com> wrote:
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
:)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
True!

Thanks, this one is even better.

Thanks,
Tinus

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:uj**************@TK2MSFTNGP15.phx.gbl...
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[i].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/<11*********************@f14g2000cwb.googlegroups. com>

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]

Nov 16 '05 #9
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[i].Name=i.ToString();
///

Just my thougth

Cor
Nov 16 '05 #10
Cor Ligthert <no************@planet.nl> wrote:
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.


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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
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
Nov 16 '05 #12
Cor Ligthert <no************@planet.nl> wrote:
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.


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.
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.


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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
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
Nov 16 '05 #14
Cor Ligthert <no************@planet.nl> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #15
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
Nov 16 '05 #16
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
Nov 16 '05 #17
Cor Ligthert <no************@planet.nl> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #18
Cor Ligthert <no************@planet.nl> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by stewart.midwinter | last post: by
4 posts views Thread by dixie | last post: by
reply views Thread by Giovane Calabrese | last post: by
8 posts views Thread by Ankit Aneja | last post: by
3 posts views Thread by B-Dog | last post: by
7 posts views Thread by rodrigo | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.