Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Cast string to button

  • 13-06-2006 10:18AM
    #1
    Registered Users, Registered Users 2 Posts: 1,454 ✭✭✭


    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 ?


Comments

  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    What language are u using? Looks like Visual Basic but can't be sure!

    [edit] actually it is I see that now.
    Do you have the buttons created?
    Could you not make a button array list?


  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout




  • Registered Users, Registered Users 2 Posts: 1,454 ✭✭✭Smoggy


    Thanks timeout , I looked at the problem a different way and came up with a solution using :

    for each ctrl in me.controls
    if crtl.name = myname then

    etc

    Thanks for looking


  • Registered Users, Registered Users 2 Posts: 684 ✭✭✭Gosh


    If you want to use the fieldname then the following will work if you call each button 'Button..."

    Dim ctl As System.Windows.Forms.Control
    
    For Each ctl In Me.Controls
       If Microsoft.VisualBasic.Left(ctl.Name, 6) = "Button" Then
          ctl.Visible = False
       End If
    Next ctl
    


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    is there not a "FindControl()" method which takes the control name as a string, and returns the control?

    i.e. Button mybutton = FindControl("myButtonsName");


  • Advertisement
Advertisement