VB中如何做到点击控件数组中的其中一个按钮并让这个按钮改变颜色
4个回答
展开全部
我才用最笨的办法,可以作为事件判断的条件
代码如下:
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
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
'属性窗口中需要设置Command1.Style = 1
Command1(Index).BackColor = vbRed
End Sub
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询