Solved

Visual Basic 2012 code to reference text box with variable

Posted on 2014-02-05
7
261 Views
Last Modified: 2014-02-06
I need to end up with this:
lblBox1.text = "X"

This is my attempt:
strBox1Position = "1"
("lblBox" & strBox1Position).text = "X"

I'm getting "syntax error"

Thanks,
Brooks
0
Comment
Question by:gbnorton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 39835989
It's all case sensitive.

I'm fairly sure if you do this in vb it's:

lblBox1.Text = "1"
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 39835992
You should read all of this: http://www.vbtutor.net/lesson3.html
0
 

Author Comment

by:gbnorton
ID: 39836046
I see I poorly described by problem.  I want to select a control by concatenating its root name with a suffix.  

lblBox1.text
lblBox2.text
lblBox3.text
...

This way I can cycle through my controls and change them.
for i = 1 to 10
"lblBox" & Cstr(i).text = "X"
next
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 39836159
Dim cnt As Integer = 1

For Each control As Control In Controls
   If TypeOf control Is Label Then
	   If control.Name = "lblBox" & cnt.ToString() Then
		   control.Text = "X"
                   cnt += 1		   
	   End If
   End If
Next control

Open in new window

0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39836185
Controls("lblBox" & strBox1Position).text = "X"
0
 
LVL 28

Expert Comment

by:Ark
ID: 39838214
strictly speaking
Container.Controls("lblBox" & strBox1Position).text = "X"
0
 

Author Closing Comment

by:gbnorton
ID: 39839063
Prefect!  Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question