473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 23488
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
4
by: dixie | last post by:
I want to write a query where one of the two tables in it is variable and its name is picked up from a text box control on an open form. The query is very simple and is only a select query with...
4
by: zlf | last post by:
Hi, When programming, some time there is a need to change some variables' name while finding a better name. However manually change it one by one is so tedious and time-consuming. What I am...
0
by: Giovane Calabrese | last post by:
ok. im try to make a multilanguage system based on that tutorial : http://www.123aspx.com/redir.aspx?res=29112 in my aspx pages all text are in labels , and i want to take the name labels...
8
by: Ankit Aneja | last post by:
I'm trying to use a for() loop to go through a set of labels and set their visibility to false. I had to do something like Code: for(int i=1;i<=10;i++) { labeli.visible = false;
3
by: B-Dog | last post by:
I'm capturing the checked radio button to XML file using the name of the radio button. I want to read my xml file to find which button was checked on close and the check the appropriate button...
7
by: rodrigo | last post by:
How would I go about retrieving a variable's name (not its value)? I want to write a function that, given a list of variables, returns a string with each variable's name and its value, like: a:...
11
by: EricGoogle | last post by:
Hello All, I am trying to figure out a how to get a variable's name from code. Ex: var MYVAR = 3; alert( ????? ); OUTPUT: "MYVAR"
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.