我才用最笨的办法,可以作为事件判断的条件
代码如下:
Private Sub Label1_Click(Index As Integer)
If Index = 0 Then
For i = 0 To 1
If i = 0 Then
Label1(i).ForeColor = vbRed
Else
Label1(i).ForeColor = vbBlack
End If
Next i
End If
If Index = 1 Then
For i = 0 To 1
If i = 1 Then
Label1(i).ForeColor = vbRed
Else
Label1(i).ForeColor = vbBlack
End If
Next i
End If
End Sub
控件数组的事件比一般的控件事件多出一个Index的参数,这个参数返回的是当前产生事件的控件的Index号,所以可以用Button(index).BackColor=?来改变颜色。(假设Button是这个控件数组的名称)
Dim mLblFlag As Boolean
Dim mLblChecked As Integer
'刷新所有lbl背景
Private Sub ClsLblBkColor(vColor As Long)
Dim oLbl As Label
For Each oLbl In Me.Label1
If oLbl.Index = mLblChecked Then
oLbl.BackColor = vbRed
Else
oLbl.BackColor = Me.BackColor
End If
Next
End Sub
Private Sub Form_Load()
'初始化选中的lbl索引号,如果想默认某个lbl一开始就是被选中的,赋值那个lbl的索引号
mLblChecked = -1
ClsLblBkColor Me.BackColor
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If mLblFlag Then
ClsLblBkColor Me.BackColor
mLblFlag = False
End If
End Sub
Private Sub Label1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1(Index).BackColor = vbRed
End Sub
Private Sub Label1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
mLblFlag = True
ClsLblBkColor Me.BackColor
If Index = mLblChecked Then Exit Sub
Label1(Index).BackColor = vbBlue
End Sub
Private Sub Label1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
mLblChecked = Index
End Sub
Private Sub Command1_Click(Index As Integer)
'属性窗口中需要设置Command1.Style = 1
Command1(Index).BackColor = vbRed
End Sub