Home All Groups Group Topic Archive Search About

how to use variable to refer to a control?

Author
13 Nov 2006 7:58 PM
Bill Nguyen
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

Author
13 Nov 2006 8:19 PM
kgerritsen
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:
Show quoteHide quote
> 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
Author
14 Nov 2006 12:39 AM
Bill Nguyen
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
Show quoteHide quote
"kgerritsen" <ki***@drexel.edu> wrote in message
news:1163449183.036368.17040@k70g2000cwa.googlegroups.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
>
Author
14 Nov 2006 5:06 AM
Cor Ligthert [MVP]
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/microsoft.public.dotnet.languages.vb?hl=en&lr=&ie=UTF-8

I am sure it was there yesterday

Cor


Show quoteHide quote
"Bill Nguyen" <billn_nospam_please@jaco.com> schreef in bericht
news:ujJpn41BHHA.4740@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
>
Author
14 Nov 2006 3:28 PM
Bill nguyen
Cor;
What am I going to do with your link?

Bill
Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:ufBphp6BHHA.5064@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/microsoft.public.dotnet.languages.vb?hl=en&lr=&ie=UTF-8
>
> I am sure it was there yesterday
>
> Cor
>
>
> "Bill Nguyen" <billn_nospam_please@jaco.com> schreef in bericht
> news:ujJpn41BHHA.4740@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
>>
>
>
Author
14 Nov 2006 5:35 PM
Cor Ligthert [MVP]
http://groups.google.com/group/microsoft.public.dotnet.languages.vb/search?hl=en&group=microsoft.public.dotnet.languages.vb&q=+how+to+use+variable+to+refer+to+a+control%3F&qt_g=1&searchnow=Search+this+group

Herfried and I have endless answered on this,

(This is not all, only your exact question)

Cor

Show quoteHide quote
"Bill nguyen" <billn_nospam_please@jaco.com> schreef in bericht
news:Oq255FACHHA.4024@TK2MSFTNGP04.phx.gbl...
> Cor;
> What am I going to do with your link?
>
> Bill
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
> news:ufBphp6BHHA.5064@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/microsoft.public.dotnet.languages.vb?hl=en&lr=&ie=UTF-8
>>
>> I am sure it was there yesterday
>>
>> Cor
>>
>>
>> "Bill Nguyen" <billn_nospam_please@jaco.com> schreef in bericht
>> news:ujJpn41BHHA.4740@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
>>>
>>
>>
>
>
Author
15 Nov 2006 1:01 AM
Bill Nguyen
Thanks.
I've only used Outlook Express to access this group.

Bill

Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:eM2lMMBCHHA.4680@TK2MSFTNGP04.phx.gbl...
> http://groups.google.com/group/microsoft.public.dotnet.languages.vb/search?hl=en&group=microsoft.public.dotnet.languages.vb&q=+how+to+use+variable+to+refer+to+a+control%3F&qt_g=1&searchnow=Search+this+group
>
> Herfried and I have endless answered on this,
>
> (This is not all, only your exact question)
>
> Cor
>
> "Bill nguyen" <billn_nospam_please@jaco.com> schreef in bericht
> news:Oq255FACHHA.4024@TK2MSFTNGP04.phx.gbl...
>> Cor;
>> What am I going to do with your link?
>>
>> Bill
>> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
>> news:ufBphp6BHHA.5064@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/microsoft.public.dotnet.languages.vb?hl=en&lr=&ie=UTF-8
>>>
>>> I am sure it was there yesterday
>>>
>>> Cor
>>>
>>>
>>> "Bill Nguyen" <billn_nospam_please@jaco.com> schreef in bericht
>>> news:ujJpn41BHHA.4740@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
>>>>
>>>
>>>
>>
>>
>
>
Author
15 Nov 2006 12:56 PM
Phill W.
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.
Author
16 Nov 2006 1:35 AM
RobinS
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.

Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> wrote in message
news:ejf2p6$ehv$1@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.
Author
16 Nov 2006 4:30 PM
Phill W.
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.IndefiniteResponses.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.
Author
16 Nov 2006 5:55 PM
RobinS
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.

Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> wrote in message
news:eji3na$d41$1@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.IndefiniteResponses.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.