Visual Basic 2010 equivalent of JavaScript numbered variable names

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robertybob
    New Member
    • Feb 2013
    • 116

    Visual Basic 2010 equivalent of JavaScript numbered variable names

    Hi

    I tried to find this via Google but as you can see from the title, I can't get my head round the correct terms to search for.

    The issue is as follows - this is an application I'm converting from javascript to VB so would like the direct solution rather than the 'array' solution if it's possible.

    In javascript...
    var myname = "Billy";
    i = 4;
    document.getEle mentById('name' +i).value = myname;
    document.getEle mentById('nameb ox'+i).checked = true;

    The first 2 lines are obviously easy in VB but what is the equivalent of the last 2 lines? I thought it was something like Me.Controls... but can't find a way to get that to work.

    Many thanks!
  • Mikkeee
    New Member
    • Feb 2013
    • 94

    #2
    You're in the right spot!
    Code:
        Private Function FindControl(ByVal controlName As String) As Control
            For Each ctrl As Control In Me.Controls
                If ctrl.Name = controlName Then
                    Return ctrl
                End If
            Next
        End Function
    OR
    Code:
    ' Returns control array of matches
    Dim ctrl() As Control = Me.Controls.Find(controlName , True)
    Last edited by Mikkeee; Feb 15 '13, 03:50 PM. Reason: Added additional method.

    Comment

    • robertybob
      New Member
      • Feb 2013
      • 116

      #3
      Many thanks, Mikkeee!

      I managed to work through your thinking and have a partial success. The issue now is that, whilst my Label text is processed, it doesn't like the checkbox code. It throws a Checkbox is not a member of Systems.Forms etc.

      A simplified version of my code is as follows - the checkbox control fails the build. The form contains Label1, Label2, Checkbox1 and Checkbox2.

      Code:
      Public Class Form1
          Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
              Dim xx = 1
              Dim thislabel = "Label" & xx
              Dim thisbox = "Checkbox" & xx
              Me.Controls(thislabel).Text = "New Text"  'perfect
              Me.Controls(thisbox).Checked = True     ' fails
          End Sub
      End Class
      All the best.

      Comment

      • Mikkeee
        New Member
        • Feb 2013
        • 94

        #4
        Code:
        DirectCast(Me.Controls(thisbox), CheckBox).Checked = True

        Comment

        • robertybob
          New Member
          • Feb 2013
          • 116

          #5
          Fantastic - thanks again, Mikkeee.

          Comment

          Working...