Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Variable in Object Name

Status
Not open for further replies.

wandan

Technical User
Dec 16, 2001
101
US
Hello,

I want to be able to run 4 sets of objects through a loop.

Example:

Dim intCnt as Integer = 1
Dim dblRate as Double
Do While intCnt <= 4
strName = "txtRate" & CStr(intCnt)
dblRate = CDbl(strName.Text)
dblGross = CStr(dblHours * 40)
lstOutput.Items.Add(dblGross)
intCnt += 1
Loop

Any suggestions on how to make this work?

 
Controls have a .Name string property. When you drag controls onto your form in the designer, it makes the .Name property the same string as the declared name of the object variable. You can access the Controls collection of your form with the Name property.

Try this
Code:
Dim intCnt as Integer = 1
Dim dblRate as Double
Do While intCnt <= 4
  strName = "txtRate" & CStr(intCnt)
  dblRate = CDbl([COLOR=#ff0000][b]Me.Controls(strName).Text[/b][/color])
  dblGross = CStr(dblHours * 40)
  lstOutput.Items.Add(dblGross)
  intCnt += 1
Loop
 
Thanks. That worked until I moved my text boxes inside of a group box. Now I am getting a message about Null Reference Unhandled. I am assuming that I need to add something for the code to find the box inside the group box but I can't figure it out. Any thoughts?
 
Replace Me.Controls with Me.TheNameOfYourGroupBox.Controls.
 
This is AWESOME!! Thank you soooo much!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top