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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable objectname

Status
Not open for further replies.

josoers

Programmer
May 13, 2003
17
0
0
NL
I have a number of objects in my form, lets say they are labels.

label1
label2
label3
label4
label5

i want to change all the labels .caption using a loop:

for x = 1 to 5
label<x>.caption = 'test'
next x

but i dont know how to do this. For example, i tried

for x = 1 to 5
tmp = 'label' + x
tmp.caption = 'test'
next x

but it doesnt work out that way

could someone tell me how to handle this one?
thanks
 
Set the objects up as a control array then you can loop through them like you wnat too.

Or you can add the controls to a container and use the

For each thing IN container

structure to loop through them.

Brian
 
I already have 16 labels named lbl_vrg1 to lbl_vrg16
What code should i use to have a loop renaming all their captions to 'test'? I really dont know how to implement the things suggested in the above reply!
 
Dim ctlLabel as Control

For each ctlLabel in Form1
If TypeName(ctlLabel) = &quot;Label&quot; Then
ctlLabel.Caption = NewLabelCaption
End If
Next ctlLabel


Craig

&quot;I feel sorry for people who don't drink. When they wake up in the morning, that's as good as they're going to feel all day.&quot;
~Frank Sinatra
 
To set up a control array, make the first object lbl_vrg

Copy this and paste it into the form and VB will ask you ig you want a control array. Say yes and then you will have two objects, lbl_vrg(0) and lbl_vrg(1). Do this 14 more time to get 16 of them and then you can use this code

for pointer = 0 to 15
lbl_vrg(pointer).caption = &quot;test&quot;
next pointer

Sorry about the first reply, I got excited. Then I had forgotten hoe to inplement the collection myself, but this is the easy way. The above reply will change the caption on all lables which is rarely what is really wanted.

Brian
 
you could use this:-

For i = 1 To 16
tempstring = &quot;lbl_vrg&quot; & i
Me.Controls(tempstring).Caption = &quot;your caption&quot;
Next i

good luck!!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top