i got 16 labels in a control array from 1 to 16, and i want the numbers 1 to 16 apear randomly in each one without any repetitions.
Could anyone help me do this? i always end up having a few empty labels and i dont know how to fix this.
Printable View
i got 16 labels in a control array from 1 to 16, and i want the numbers 1 to 16 apear randomly in each one without any repetitions.
Could anyone help me do this? i always end up having a few empty labels and i dont know how to fix this.
this should work:
VB Code:
Private Sub Command1_Click() Randomize For i = Label1.LBound To Label1.UBound Label1(i).Caption = "" 'empty all captions Next For i = Label1.LBound To Label1.UBound ReRnd: TempIndex = Round(Rnd * Label1.UBound) 'new random nr If Label1(TempIndex).Caption = "" Then 'if caption is empty Label1(TempIndex).Caption = i + 1 'fill it with nr Else GoTo ReRnd 'if caption is already filled, get a new label End If Next End Sub
oh....your array is from 1-16....then you have to change the line
Label1(TempIndex).Caption = i + 1
to
Label1(TempIndex).Caption = i
oh, and you also have to change the line:
TempIndex = Round(Rnd * Label1.UBound)
to
TempIndex = Round(Rnd * (Label1.UBound - 1)) + 1