Solved

Visual Basic 2012 code to reference text box with variable

Posted on 2014-02-05
7
255 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
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net class 3 28
Crystal reports login after closing form 3 34
VB.Net - BackgroundWorker running multiple 5 46
Need help with another query 10 34
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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