|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Access object through String ?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. 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."Cerebrus99" <zorg***@sify.com> schrieb: Accessing controls by their names or indices> 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) <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/>
Best data structure?
empty GUID from string in VB2005 Changing CheckedState or Checked-value without raising ItemChecked-event Installing published application fails Create object through classname How to change "database and User" into CR9 Webservice MyApplication has encountered a problem... when not Administrator! Opacity Unable to open file Control Arrays |
|||||||||||||||||||||||