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

LabelX.Caption??

Status
Not open for further replies.

PockyBum522

Programmer
Jun 10, 2003
239
US
I have 4 labels, label1, label2 etc...I want to use a for loop to cycle through them, how do I put a variable in the name for label1.caption, instead of 1, X or something. Here's what I have:

Label(ctr + 1).Caption = Arr(ctr)

I assume I'm just forgetting something obvious, if anyone could tell me how to to this it would be most appreciated >.<
 
Do a search for &quot;control array&quot; you'll find many, many examples.

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
You can use form's Controls collection:
Me.Controls(&quot;Label&quot; & i).Caption = arr(i)

VB has special feature called Control Array, which can simplify coding. To create Control Array, give two labels the same name, or set Index property of the label to some number.
You'll be able to use this syntax:

Label(i).Caption = arr(i)

or, using form's Controls collection:
Me.Controls(&quot;Label&quot;, i).Caption = arr(i)
 
Your labels must be also set up as an array. Rename your labels like the following:

label1 and set the index property to 0
label1 and set the index property to 1, etc, etc

then you may loop through them.
Code:
dim Cnt as integer
for Cnt= 0 to label1.ubound
label1(Cnt).caption = arr(Cnt)
next

hope it helps.

***You can't change your past, but you can change your future***
 
Thank you, I knew there was something I was forgetting, the controls collection looks kinda cool but the control array was what I was looking for, Thank you both!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top