By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,534 Members | 910 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,534 IT Pros & Developers. It's quick & easy.

how to use variable to refer to a control?

P: n/a
I have several textbox controls in a form,.
I name them txt1, txt2, ... txt10.
How dow I refer to them in a loop to get the value in text proerty of each
control?

For x = 1 to 10

value = txt(x).text

next x

Thanks

Bill
Nov 13 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Since I try to give my controls business-meaning names, I don't iterate
based on an integer index. I have had some success organizing
similar-ruled controls within containers such as panels or tabcontrol
tabpages and using the container controls collection.

Dim textitem as textbox
For each textitem in form1.controls
'do stuff
next textitem

For each textitem in panel1.controls
'do stuff
next textitem

For each textitem in tabcontrol1.tabpages(1).controls
'do stuff
next textitem

If I have to apply a differential rule based on the exact name, an if
or select case on textitem.name will work.

HTH,
Keith

Bill Nguyen wrote:
I have several textbox controls in a form,.
I name them txt1, txt2, ... txt10.
How dow I refer to them in a loop to get the value in text proerty of each
control?

For x = 1 to 10

value = txt(x).text

next x

Thanks

Bill
Nov 13 '06 #2

P: n/a
Keith;

This is great.
However, I still need to be able to refer to an actual control name.
Is there a way to do that?

Thanks again

Billl
"kgerritsen" <ki***@drexel.eduwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
Since I try to give my controls business-meaning names, I don't iterate
based on an integer index. I have had some success organizing
similar-ruled controls within containers such as panels or tabcontrol
tabpages and using the container controls collection.

Dim textitem as textbox
For each textitem in form1.controls
'do stuff
next textitem

For each textitem in panel1.controls
'do stuff
next textitem

For each textitem in tabcontrol1.tabpages(1).controls
'do stuff
next textitem

If I have to apply a differential rule based on the exact name, an if
or select case on textitem.name will work.

HTH,
Keith

Bill Nguyen wrote:
>I have several textbox controls in a form,.
I name them txt1, txt2, ... txt10.
How dow I refer to them in a loop to get the value in text proerty of
each
control?

For x = 1 to 10

value = txt(x).text

next x

Thanks

Bill

Nov 14 '06 #3

P: n/a
Bill,

Did you ever Google this newsgroup, your question comes almost twice a day
and has forever almost three answers. This time I do it like this.

http://groups.google.com/group/micro...n&lr=&ie=UTF-8

I am sure it was there yesterday

Cor
"Bill Nguyen" <bi*****************@jaco.comschreef in bericht
news:uj**************@TK2MSFTNGP03.phx.gbl...
>I have several textbox controls in a form,.
I name them txt1, txt2, ... txt10.
How dow I refer to them in a loop to get the value in text proerty of each
control?

For x = 1 to 10

value = txt(x).text

next x

Thanks

Bill

Nov 14 '06 #4

P: n/a
Cor;
What am I going to do with your link?

Bill
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uf**************@TK2MSFTNGP02.phx.gbl...
Bill,

Did you ever Google this newsgroup, your question comes almost twice a day
and has forever almost three answers. This time I do it like this.

http://groups.google.com/group/micro...n&lr=&ie=UTF-8

I am sure it was there yesterday

Cor
"Bill Nguyen" <bi*****************@jaco.comschreef in bericht
news:uj**************@TK2MSFTNGP03.phx.gbl...
>>I have several textbox controls in a form,.
I name them txt1, txt2, ... txt10.
How dow I refer to them in a loop to get the value in text proerty of
each control?

For x = 1 to 10

value = txt(x).text

next x

Thanks

Bill


Nov 14 '06 #5

P: n/a
http://groups.google.com/group/micro...rch+this+group

Herfried and I have endless answered on this,

(This is not all, only your exact question)

Cor

"Bill nguyen" <bi*****************@jaco.comschreef in bericht
news:Oq**************@TK2MSFTNGP04.phx.gbl...
Cor;
What am I going to do with your link?

Bill
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uf**************@TK2MSFTNGP02.phx.gbl...
>Bill,

Did you ever Google this newsgroup, your question comes almost twice a
day and has forever almost three answers. This time I do it like this.

http://groups.google.com/group/micro...n&lr=&ie=UTF-8

I am sure it was there yesterday

Cor
"Bill Nguyen" <bi*****************@jaco.comschreef in bericht
news:uj**************@TK2MSFTNGP03.phx.gbl...
>>>I have several textbox controls in a form,.
I name them txt1, txt2, ... txt10.
How dow I refer to them in a loop to get the value in text proerty of
each control?

For x = 1 to 10

value = txt(x).text

next x

Thanks

Bill



Nov 14 '06 #6

P: n/a
Thanks.
I've only used Outlook Express to access this group.

Bill

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:eM**************@TK2MSFTNGP04.phx.gbl...
http://groups.google.com/group/micro...rch+this+group

Herfried and I have endless answered on this,

(This is not all, only your exact question)

Cor

"Bill nguyen" <bi*****************@jaco.comschreef in bericht
news:Oq**************@TK2MSFTNGP04.phx.gbl...
>Cor;
What am I going to do with your link?

Bill
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uf**************@TK2MSFTNGP02.phx.gbl...
>>Bill,

Did you ever Google this newsgroup, your question comes almost twice a
day and has forever almost three answers. This time I do it like this.

http://groups.google.com/group/micro...n&lr=&ie=UTF-8

I am sure it was there yesterday

Cor
"Bill Nguyen" <bi*****************@jaco.comschreef in bericht
news:uj**************@TK2MSFTNGP03.phx.gbl...
I have several textbox controls in a form,.
I name them txt1, txt2, ... txt10.
How dow I refer to them in a loop to get the value in text proerty of
each control?

For x = 1 to 10

value = txt(x).text

next x

Thanks

Bill



Nov 15 '06 #7

P: n/a
Bill Nguyen wrote:
I have several textbox controls in a form,.
Good.
I name them txt1, txt2, ... txt10.
Not so good.
How dow I refer to them in a loop to get the value in text proerty of each
control?
(I love this answer; it /so/ annoys the .Net purists)

You use a Control Array. :-)

No, seriously.

You create an array of Controls, in this case TextBoxes, and loop
through that.

Dim textboxes As TextBox() = { txt1, txt2, ... txt10 }

For Each tb As TextBox in textboxes
value = tb.Text
Next

Regards,
Phill W.
Nov 15 '06 #8

P: n/a
I'm a .Net purist, and I think your answer is brilliant.
It's simple and concise, and gets the job done.

Does that mean I'm really *not* a .Net purist? :-(

Robin S.

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ej**********@south.jnrs.ja.net...
Bill Nguyen wrote:
>I have several textbox controls in a form,.

Good.
>I name them txt1, txt2, ... txt10.

Not so good.
>How dow I refer to them in a loop to get the value in text proerty of
each control?

(I love this answer; it /so/ annoys the .Net purists)

You use a Control Array. :-)

No, seriously.

You create an array of Controls, in this case TextBoxes, and loop through
that.

Dim textboxes As TextBox() = { txt1, txt2, ... txt10 }

For Each tb As TextBox in textboxes
value = tb.Text
Next

Regards,
Phill W.

Nov 16 '06 #9

P: n/a
RobinS wrote:
I'm a .Net purist, and I think your answer is brilliant.
It's simple and concise, and gets the job done.

Does that mean I'm really *not* a .Net purist? :-(
? Microsoft.Interaction.Responses.IndefiniteResponse s.Maybe

It used to be the case that mentioning "Control Array" (a very specific
VB "Proper" concept) in a .Net group brought down all manner of scorn of
the head of the unwary poster.

I suspect the previously vehement VB bashers are all off trying to work
out what their programs are doing now that they've dived into these
Generics things ... ;-)

Regards,
Phill W.
Nov 16 '06 #10

P: n/a
Well, there's no doubt that Generics are very cool.
But that doesn't mean there's no place in the world
for an array or arraylist. After all, even though
there are plasma tv's out there, some people still
rely on their Sony Wegas to do the job.

Robin S.

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ej**********@south.jnrs.ja.net...
RobinS wrote:
>I'm a .Net purist, and I think your answer is brilliant.
It's simple and concise, and gets the job done.

Does that mean I'm really *not* a .Net purist? :-(

? Microsoft.Interaction.Responses.IndefiniteResponse s.Maybe

It used to be the case that mentioning "Control Array" (a very specific VB
"Proper" concept) in a .Net group brought down all manner of scorn of the
head of the unwary poster.

I suspect the previously vehement VB bashers are all off trying to work
out what their programs are doing now that they've dived into these
Generics things ... ;-)

Regards,
Phill W.

Nov 16 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.