Home All Groups Group Topic Archive Search About

Access object through String ?

Author
20 Feb 2006 2:33 PM
Cerebrus99
Hi,

Say, I have 20 textboxes on my form placed in different panels, groupboxes.
The names of the textboxes range from "TextBox1" to "TextBox20".

I want to get a reference to each textbox using it's already known name, as
in :
-------------------------------------------------------
Dim i As Integer
For i = 1 To 20
Dim txt As TextBox = CType("Textbox" & i, TextBox)
                                                ^^^^^^^^^^
txt.Text = "Number " & i
Next
-------------------------------------------------------
But of course, the above code doesn't even compile, since a String cannot be
converted to a System.Windows.Forms.TextBox. Of course, I could use other
options(Such as For-each loops), but my main purpose is not that. (See
below)

So, my question actually is: How do I use a dynamically constructed string,
containing the name of an Object (or Control) to get a reference to the
Object itself ?

Regards,

Cerebrus.

Author
20 Feb 2006 2:38 PM
Cerebrus99
Oh Never mind !

I see someone asked the very same question at this very same time and
already got a reply !

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_thread/thread/596d549862308882/492b99dd65a427d1#492b99dd65a427d1

Some coincidence !

:-)

Cerbrus.
Author
20 Feb 2006 8:32 PM
Herfried K. Wagner [MVP]
"Cerebrus99" <zorg***@sify.com> schrieb:
> Say, I have 20 textboxes on my form placed in different panels,
> groupboxes.
> The names of the textboxes range from "TextBox1" to "TextBox20".
>
> I want to get a reference to each textbox using it's already known name,
> as
> in :
>[...]
> Dim txt As TextBox = CType("Textbox" & i, TextBox)

Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>