Private WithEvents MyCtrl As MsForms.CommandButton
Private MyIndex As Integer
Private MyCaller As Object 'サブルーチンCallの為にUserForm変数をObject 型にする必要あり
Public Property Let Item(NewCtrl As MsForms.CommandButton)
Set MyCtrl = NewCtrl
End Property
Public Property Let Index(NewIndex As Integer)
MyIndex = NewIndex
End Property
Public Property Let Caller(NewCaller As Object)
Set MyCaller = NewCaller
End Property
Private Sub MyCtrl_Click()
Call MyCaller.RaiseClick(MyIndex) 'MyCallerはObject型のUserForm参照
End Sub
===== UserForm1 モジュール =====
Private colWeekBtn As Collection
Private cmdWeekBtn(1 To 7) As clsCmdWeek
Private Sub UserForm_Initialize()
Dim i As Integer
Set colWeekBtn = New Collection ' インスタンスの生成
With colWeekBtn
.Add cmdSun
:
.Add cmdSat
End With
For i = 1 To 7
Set cmdWeekBtn(i) = New clsCmdWeek ' インスタンスの生成
With cmdWeekBtn(i)
.Item = colWeekBtn(i)
.Index = i .Caller = Me
End With
Next i
End Sub
Public Sub RaiseClick(ByVal Index As Integer)
Dim vntWeekName As Variant
vntWeekName = Array("", "日", "月", "火",
"水", "木", "金", "土")
MsgBox vntWeekName(Index) & "曜日ボタンがクリックされました(" & Index & ")"
If (colWeekBtn(Index).BackColor = vbButtonFace) Then
colWeekBtn(Index).BackColor = vbRed
Else
colWeekBtn(Index).BackColor = vbButtonFace
End If
End Sub
( 中略 )
Public Property Get Item() As MsForms.CommandButton
Set Item = MyCtrl
End Property
( 中略 )
===== UserForm1 モジュール =====
Private cmdWeekBtn(1 To 7) As clsCmdWeek
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 7
Set cmdWeekBtn(i) = New clsCmdWeek ' インスタンスの生成
With cmdWeekBtn(i)
.Item = Me.Controls("cmdWeek" & i)
.Index = i
.Caller = Me
End With
Next i
End Sub
Public Sub RaiseClick ( ByVal Index As Integer )
Dim vntWeekName As Variant
vntWeekName = Array("", "日", "月", "火",
"水", "木", "金", "土")
MsgBox vntWeekName(Index) & "曜日ボタンがクリックされました(" & Index & ")"
If (cmdWeekBtn(Index).Item.BackColor = vbButtonFace) Then
cmdWeekBtn(Index).Item.BackColor = vbRed
Else
cmdWeekBtn(Index).Item.BackColor =
vbButtonFace
End If
End Sub