Solved

Visual Basic 2012 code to reference text box with variable

Posted on 2014-02-05
7
254 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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