两个label数组(各15个label)
Private Sub Form_Load()
Dim i, j As Integer
For i = 0 To 14
Randomize
j = Int(3 * Rnd)
If j = 0 Then
Label1(i).Caption = "A"
Label2(i).Caption = "1"
ElseIf j = 1 Then
Label1(i).Caption = "B"
Label2(i).Caption = "2"
Else
Label1(i).Caption = "C"
Label2(i).Caption = "3"
End If
Next i
End Sub 要求出了A、B、C之后
例如label数组出ABC的情况
A(0) C(3) B(6) C(9) A(12) B(1) A(4) C(7) A(10) C(13) B(2) A(5) C(8) A(11) A(14)
括号中表示label1数组的索引
对应的在label2在form中的坐标是
0 1 4 6 10
2 5 7 11
3 8 12
9 13
14
label2(0)的初始坐标是(0,0)
就是说如果出A,后面出B的话,left+120,top为0
如果出A,后面出C的话,那么C的left不变,top+120,如果C后面继续出A的话那么第二个A的left与C的left一样,若C后面出B的话,那么B的left是C的left+120,top为0
Private Sub Form_Load()
Dim i, j As Integer
For i = 0 To 14
Randomize
j = Int(3 * Rnd)
If j = 0 Then
Label1(i).Caption = "A"
Label2(i).Caption = "1"
ElseIf j = 1 Then
Label1(i).Caption = "B"
Label2(i).Caption = "2"
Else
Label1(i).Caption = "C"
Label2(i).Caption = "3"
End If
Next i
End Sub 要求出了A、B、C之后
例如label数组出ABC的情况
A(0) C(3) B(6) C(9) A(12) B(1) A(4) C(7) A(10) C(13) B(2) A(5) C(8) A(11) A(14)
括号中表示label1数组的索引
对应的在label2在form中的坐标是
0 1 4 6 10
2 5 7 11
3 8 12
9 13
14
label2(0)的初始坐标是(0,0)
就是说如果出A,后面出B的话,left+120,top为0
如果出A,后面出C的话,那么C的left不变,top+120,如果C后面继续出A的话那么第二个A的left与C的left一样,若C后面出B的话,那么B的left是C的left+120,top为0
Dim Label1(0 To 14) As Object
Dim Label2(0 To 14) As Object
Dim i As Integer
Dim intLeft As Integer
Dim intTop As Integer
Dim intFirstTop As Integer
Dim strValue As String
Dim strOldValue As String
'动态创建控件数组,如果你已经有可以去掉
For i = 0 To 14
Set Label1(i) = Me.Controls.Add("VB.Label", "Label1" & i)
Set Label2(i) = Me.Controls.Add("VB.Label", "Label2" & i)
Next i
'随机获取Caption
For i = 0 To 14
Randomize
j = Int(3 * Rnd)
If j = 0 Then
Label1(i).Caption = "A"
Label2(i).Caption = "1"
ElseIf j = 1 Then
Label1(i).Caption = "B"
Label2(i).Caption = "2"
Else
Label1(i).Caption = "C"
Label2(i).Caption = "3"
End If
Next i
'排列
intFirstTop = 0
intTop = intFirstTop
intLeft = 0
strValue = "0"
strOldValue = "0"
For i = 0 To 14
If strValue <> "0" Then
If Label2(i).Caption = "3" Then
intTop = intTop + 200
intLeft = intLeft
Else
If strValue <> "3" Then
If Label2(i).Caption = strValue Then
intTop = intTop + 200
intLeft = intLeft
Else
intTop = intFirstTop
intLeft = intLeft + 200
End If
Else
If strOldValue = "0" Or strOldValue = "3" Then
intTop = intTop + 200
intLeft = intLeft
Else
If Label2(i).Caption = strOldValue Then
intTop = intTop + 200
intLeft = intLeft
Else
intTop = intFirstTop
intLeft = intLeft + 200
End If
End If
End If
End If
End If
Label2(i).Move intLeft, intTop
Label2(i).AutoSize = True
Label2(i).Visible = True
strOldValue = strValue
strValue = Label2(i).Caption
Next i
End Sub