PDA

View Full Version : Solved: VB6 - set form item value using variable names?



andrew93
08-20-2006, 02:36 AM
Hi

I'm working on a VB6 project and am seeking an elegant solution to some messy coding, if possible. My form is displaying 36 pairs of outcomes based on various things, including 2 dice results. I am showing the results in a 6x6 grid made up of 36 text boxes labelled "Outcome1" through to "Outcome36". The 36 pairs of results are stored in arrays, ie. Dice1(35) & Dice2(35). Is it possible to use a loop to select each 'Outcome' text box? At the moment my code looks like this :


Outcome1.Caption = Dice1(0) & ", " & Dice2(0)
Outcome2.Caption = Dice1(1) & ", " & Dice2(1)
Outcome3.Caption = Dice1(2) & ", " & Dice2(2)
'....etc etc etc
Is there a smarter way of doing this with a loop? If so, how do I get the code to select the correct outcome box based on a loop counter?

Thanks in advance
Andrew

Tommy
08-20-2006, 07:16 AM
Just as a suggestion, I would make a control array of labels/textboxes. then loop,


Dim Cntr as Long
For Cntr=0 to 35
Outcome(Cntr).Caption = Dice1(Cntr) & ", " & Dice2(Cntr)
Next

HTH

mdmackillop
08-20-2006, 09:46 AM
... or maybe

k = 0
For i = 1 To 6
For j = 1 To 6
k = k + 1
Me.Controls("Outcome" & k).Caption = i & ", " & j
Next
Next

Bob Phillips
08-21-2006, 02:11 AM
AS you are using VB6, have you setup the textboxes as a control array?

andrew93
08-21-2006, 02:27 AM
Thanks everyone. The control array did the trick!
Cheers
Andrew