Home All Groups Group Topic Archive Search About

How 2 refer to controls by a string variable name instead of index

Author
20 Feb 2006 2:24 PM
GY2
How do I refer to controls by a string variable name instead of by integer
item index?

A form contains 7 rich textbox controls labeled rtb1-rtb7. In VB6 I could
use the following code to loop through each one but with .NET I'm having
trouble doing something just as efficient.

Is there not a way to refer to the control by its name or do I have to go
find its index number in order to point to it?

All help appreciated.


        For K = 1 To 7
            strWhichRTB = "rtb" & K
            With Me(strWhichRTB)
                .Text = "whatever"
            End With
        Next K

Author
20 Feb 2006 2:30 PM
Peter Proost
Hi,

check this link:

http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

Show quoteHide quote
"GY2" <2muchspam@wherever.com> schreef in bericht
news:u9MGjliNGHA.3732@TK2MSFTNGP10.phx.gbl...
> How do I refer to controls by a string variable name instead of by integer
> item index?
>
> A form contains 7 rich textbox controls labeled rtb1-rtb7. In VB6 I could
> use the following code to loop through each one but with .NET I'm having
> trouble doing something just as efficient.
>
> Is there not a way to refer to the control by its name or do I have to go
> find its index number in order to point to it?
>
> All help appreciated.
>
>
>         For K = 1 To 7
>             strWhichRTB = "rtb" & K
>             With Me(strWhichRTB)
>                 .Text = "whatever"
>             End With
>         Next K
>
>
>
Author
20 Feb 2006 3:23 PM
GY2
Thanks a lot. This shows several ways to do it and I will probably go with
an array. Even so none of these methods are as clean as the old VB 6 way in
my opinion.

Show quoteHide quote
"Peter Proost" <pproost@nospam.hotmail.com> wrote in message
news:e$aB6piNGHA.740@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> check this link:
>
> http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en
>
> Greetz Peter
>
> --
> Programming today is a race between software engineers striving to build
> bigger and better idiot-proof programs, and the Universe trying to produce
> bigger and better idiots. So far, the Universe is winning. (Rich Cook)
>
> "GY2" <2muchspam@wherever.com> schreef in bericht
> news:u9MGjliNGHA.3732@TK2MSFTNGP10.phx.gbl...
>> How do I refer to controls by a string variable name instead of by
>> integer
>> item index?
>>
>> A form contains 7 rich textbox controls labeled rtb1-rtb7. In VB6 I could
>> use the following code to loop through each one but with .NET I'm having
>> trouble doing something just as efficient.
>>
>> Is there not a way to refer to the control by its name or do I have to go
>> find its index number in order to point to it?
>>
>> All help appreciated.
>>
>>
>>         For K = 1 To 7
>>             strWhichRTB = "rtb" & K
>>             With Me(strWhichRTB)
>>                 .Text = "whatever"
>>             End With
>>         Next K
>>
>>
>>
>
>