By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

Visual Basic 2010 equivalent of JavaScript numbered variable names

100+
P: 116
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.getElementById('name'+i).value = myname;
document.getElementById('namebox'+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!
Feb 15 '13 #1

✓ answered by Mikkeee

You're in the right spot!
Expand|Select|Wrap|Line Numbers
  1.     Private Function FindControl(ByVal controlName As String) As Control
  2.         For Each ctrl As Control In Me.Controls
  3.             If ctrl.Name = controlName Then
  4.                 Return ctrl
  5.             End If
  6.         Next
  7.     End Function
  8.  
OR
Expand|Select|Wrap|Line Numbers
  1. ' Returns control array of matches
  2. Dim ctrl() As Control = Me.Controls.Find(controlName , True)
  3.  

Share this Question
Share on Google+
4 Replies


Mikkeee
P: 94
You're in the right spot!
Expand|Select|Wrap|Line Numbers
  1.     Private Function FindControl(ByVal controlName As String) As Control
  2.         For Each ctrl As Control In Me.Controls
  3.             If ctrl.Name = controlName Then
  4.                 Return ctrl
  5.             End If
  6.         Next
  7.     End Function
  8.  
OR
Expand|Select|Wrap|Line Numbers
  1. ' Returns control array of matches
  2. Dim ctrl() As Control = Me.Controls.Find(controlName , True)
  3.  
Feb 15 '13 #2

100+
P: 116
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.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  3.         Dim xx = 1
  4.         Dim thislabel = "Label" & xx
  5.         Dim thisbox = "Checkbox" & xx
  6.         Me.Controls(thislabel).Text = "New Text"  'perfect
  7.         Me.Controls(thisbox).Checked = True     ' fails
  8.     End Sub
  9. End Class
  10.  
All the best.
Feb 15 '13 #3

Mikkeee
P: 94
Expand|Select|Wrap|Line Numbers
  1. DirectCast(Me.Controls(thisbox), CheckBox).Checked = True
  2.  
Feb 15 '13 #4

100+
P: 116
Fantastic - thanks again, Mikkeee.
Feb 15 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.