cast from string to button

  • Thread starter Thread starter Jon Vaughan
  • Start date Start date
J

Jon Vaughan

Im 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
 
Back
Top