内容相同的label为一种颜色
在学习VB实践中碰到这样一个问题:在窗体中有40个label(label1到label40),每个label中都显示了三个字符(如:40R、43J、44P等)
如何让相同内容的label显示一种颜色
(如:Label.caption=70R的为一种颜色
Label.caption=43J的为一种颜色
Label.caption=B4P的为一种颜色
……)
请各高手赐教……
Option Explicit Private Sub Command1_Click() Dim k(1 To 3) As String Dim c(1 To 3) As Long Dim i As Long Dim o As Object k(1) = "40R" k(2) = "43J" k(3) = "44P" '随机填内容 ,针对所有的 标签 For Each o In Form1.Controls If TypeName(o) = "Label" Then i = 1 + Int(Rnd() * 3) o.Caption = k(i) End If Next For i = 1 To 3 '产生随机颜色 c(i) = Int(Rnd() * 16777216) Next i '根据内容设置颜色 For Each o In Form1.Controls If TypeName(o) = "Label" Then Select Case o.caption Case "40R" o.ForeColor = c(1) Case "43J" o.ForeColor = c(2) Case "44P" o.ForeColor = c(3) End Select End If Next End Sub