|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
cast from string to buttonIm trying to make some code that can reference x amount of buttons and then
switch visible to false For x = 1 To 10 btnSelected = CType("btnButton" & x.ToString, Button) btnSelected.Visible = False Next Provides the error : Value of type 'String' cannot be converted to 'System.Windows.Forms.Button'. Any ideas, about converting string to object types such as buttons ? Jon,
If you will add this string on runtime, than the best approach is to add the values from the string to the Tag of the button. (typed here so watch typos or whatever) Than you get For each ctr in controls if typeof ctr Is button then if ctr.tag.toString = TheGivenString btnSelected.Visible = False end if end if Next This goes fast, you can do it as well with late binding using reflection but that will cost you performance. In development time this kind of instructions are in my opinion without sense when you know the name of a control than there is no need to use a string. (It looks than like a misplaced way of using scripting). I hope this helps, Cor Thanks cor , helped alot
Show quoteHide quote "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message news:%23259y6sjGHA.4444@TK2MSFTNGP02.phx.gbl... > Jon, > > If you will add this string on runtime, than the best approach is to add > the values from the string to the Tag of the button. (typed here so watch > typos or whatever) > > Than you get > > For each ctr in controls > if typeof ctr Is button then > if ctr.tag.toString = TheGivenString > btnSelected.Visible = False > end if > end if > Next > > > This goes fast, you can do it as well with late binding using reflection > but that will cost you performance. > > In development time this kind of instructions are in my opinion without > sense when you know the name of a control than there is no need to use a > string. (It looks than like a misplaced way of using scripting). > > I hope this helps, > > Cor > "Jon Vaughan" <jonnyvaug***@hotmail.com> schrieb: Accessing controls by their names or indices> btnSelected = CType("btnButton" & x.ToString, Button) <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/>
RANT: option strict etc
Handling DBNull from databases Function Vs. Sub Procedure Final Post of the day getting screen coordinates for positioning a floating control next to a tableLayOutPanel embeded con Loopin trough colors Setting the Title property in a Windows File ... A Question About Regular Expressions and Capture Emulating keyboard strokes in vb.net Refer to sheet2 - even when named |
|||||||||||||||||||||||