|
1、Classを使わない通常の方法
Labelは1〜3とします
Option Explicit
'設定するLableの個数
Private Const clngNumb As Long = 3
Private vntLetter As Variant
Private Sub UserForm_Initialize()
Dim i As Long
vntLetter = Array("○", "△", "□")
For i = 1 To clngNumb
Controls("Lable" & i).Tag = 0
Next i
End Sub
Private Sub Label1_Click()
Dim lngCount As Long
lngCount = (UBound(vntLetter) + 1)
With Label1
.Tag = (.Tag + 1) Mod lngCount
.Caption = vntLetter(.Tag Mod lngCount)
End With
End Sub
Private Sub Label2_Click()
Dim lngCount As Long
lngCount = (UBound(vntLetter) + 1)
With Label2
.Tag = (.Tag + 1) Mod lngCount
.Caption = vntLetter(.Tag Mod lngCount)
End With
End Sub
Private Sub Label3_Click()
Dim lngCount As Long
lngCount = (UBound(vntLetter) + 1)
With Label3
.Tag = (.Tag + 1) Mod lngCount
.Caption = vntLetter(.Tag Mod lngCount)
End With
End Sub
2、Classを使った方法
UserFormのコードモジュールに以下を記述
Option Explicit
'設定するLableの個数
Private Const clngNumb As Long = 3
'Groupの値取得クラスの配列
Private clsLable() As Class1
Private Sub UserForm_Initialize()
Dim i As Long
Dim vntLetter As Variant
vntLetter = Array("○", "△", "□")
'LableのTagに初期値とClassを設定
ReDim Preserve clsLable(1 To clngNumb)
For i = 1 To clngNumb
Controls("Label" & i).Tag = 0
Set clsLable(i) = New Class1
With clsLable(i)
.Letter = vntLetter
.LabelCnt = Controls("Label" & i)
End With
Next i
End Sub
Private Sub UserForm_Terminate()
Dim i As Long
For i = 1 To clngNumb
Set clsLable(i) = Nothing
Next i
End Sub
「挿入」→「クラスモジュール」でClass1を追加
Class1に以下を記述
Option Explicit
Private WithEvents lblMark As MSForms.Label
Private vntLetter As Variant
Private Sub Class_Terminate()
Set lblMark = Nothing
End Sub
Private Sub lblMark_Click()
Dim lngCount As Long
lngCount = (UBound(vntLetter) + 1)
With lblMark
.Tag = (.Tag + 1) Mod lngCount
.Caption = vntLetter(.Tag Mod lngCount)
End With
End Sub
Public Property Let Letter(ByVal vntNewValue As Variant)
vntLetter = vntNewValue
End Property
Public Property Let LabelCnt(ByVal lblNewValue As MSForms.Label)
Set lblMark = lblNewValue
End Property
|
|