cast from string to button

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Vaughan

    #1

    cast from string to button

    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("btnButto n" & x.ToString, Button)

    btnSelected.Vis ible = 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 ?


  • Cor Ligthert [MVP]

    #2
    Re: cast from string to button

    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.toStrin g = TheGivenString
    btnSelected.Vis ible = 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


    Comment

    • Jon Vaughan

      #3
      Re: cast from string to button

      Thanks cor , helped alot

      "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
      news:%23259y6sj GHA.4444@TK2MSF TNGP02.phx.gbl. ..[color=blue]
      > 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.toStrin g = TheGivenString
      > btnSelected.Vis ible = 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
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: cast from string to button

        "Jon Vaughan" <jonnyvaughan@h otmail.com> schrieb:[color=blue]
        > btnSelected = CType("btnButto n" & x.ToString, Button)[/color]

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

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

        Comment

        Working...