I have 96 blank labels on my form.
(label106 to lable201).
I want to use arrays to generate numbers to be randomly filled in these labels.
How can I go about doing this.
Thank you.
Printable View
I have 96 blank labels on my form.
(label106 to lable201).
I want to use arrays to generate numbers to be randomly filled in these labels.
How can I go about doing this.
Thank you.
Well, you shouldnt have the labels named like that.
They should be label(106) to label(201).
To fill with random numbers do :
- jamieCode:Dim i As Long
For i = 0 to 95
Label(106 + i).Caption = Rnd
Next i
Well a faster loop would be this I'd imagine :
Code:Dim i As Long
For i = 106 to 201
Label(i).Caption = Rnd
Next i
First, i think you will ither have to delete all of your label controls, copy one of them and paste it, answering YES to the popup box, asking about making a control array.
the other way i think would to rename them all... and make them into an array manualy that way,
dunno tho.... im probly missing an easyer meathod, but thats the only way ive known
then you would just go ......
Const LowNumber = 1
Const HighNumber = 50
Private Sub Command1_Click()
For i = 1 To 96
Label(i).Caption = Int((HighNumber - LowNumber + 1) * Rnd + LowNumber)
Next
End Sub
err... change the line
For i = 1 To 96
to
For i = 0 To 95
arrays start at 0 :P
You should convert (via renaming or deleting or whatever) all of the labels, and make a control array.
But there is another way.
If you use the scripting control, it allows you to add arbitrary code to your app at runtime.
So you could actually do something like :
"Label" & (106 + i) & ".Caption = Rnd"
Then you'd execute the code at runtime, and it'd work.
Trust me :)
- jamie
Hi,
Thanks but how do I get it to print the values on the form in each of the labels.
The form just has labels on it and I need numbers to be displayed on the labels.
the labels are named label106 all the way to label206.
thank you.
This one doesnt :)Quote:
Originally posted by Ph34R
err... change the line
For i = 1 To 96
to
For i = 0 To 95
arrays start at 0 :P
God I'm really annoying arent I :)Code:Dim var_string(25 To 100) As String
Dim i As Long
For i = 25 To 100
var_string(i) = "osajdoiasjosdiaj"
Next i
- jamie
Shu.
Delete all of your labels.
Add a new label.
Copy and paste it.
Allow it to use a control array.
The use our code.
- jamie
plenderj,
I can't create a control array for my labels because this is just part of my application and I need the labels to be seperate.
Isn't there any way that I could display blank labels on a form with with random numbers.
thank you.
Well yeah, you could just simply do this :
Except a control array would be much slicker.Code:Label106.Caption = Rnd
Label107.Caption = Rnd
Label108.Caption = Rnd
Label109.Caption = Rnd
Label110.Caption = Rnd
Label111.Caption = Rnd
Label112.Caption = Rnd
Label113.Caption = Rnd
'...
Label201.Caption = Rnd
Also, as I said you could use the microsoft scripting engine, but thats another story.
- jamie