Solved

Visual Basic 2012 code to reference text box with variable

Posted on 2014-02-05
7
263 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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