'**************************************************************
Option Explicit
Private WithEvents ctrl As MSForms.CommandButton
'Constructor
Private Sub Class_Initialize()
End Sub
'FreeMem/Dispose
Private Sub Class_Terminate()
If Not ctrl Is Nothing Then
'ctrl.Delete
Set ctrl = Nothing
End If
End Sub
'-------------------------------------------------
'Methods
Public Sub Add(Form As MSForms.UserForm, Optional ByVal strCaption As String = "")
Set ctrl = Form.Controls.Add("Forms.CommandButton.1")
If strCaption <> "" Then
ctrl.Caption = strCaption
End If
End Sub
'-------------------------------------------------
Public Sub Wrap(ByVal ctrlCoomandButton As Object, Optional ByVal strCaption As String = "")
Set ctrl = ctrlCoomandButton
If strCaption <> "" Then
ctrl.Caption = strCaption
End If
End Sub
'-------------------------------------------------
'Properties
'Property Value
'-------------------------------------------------
Public Property Let Caption(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
ctrl.Caption = vData
End Property
Public Property Get Caption() As String
'used when retrieving value of a property, on the right side of an assignment.
Caption = ctrl.Caption
End Property
'-------------------------------------------------
Public Property Let ForeColor(ByVal vData As Long)
'used when assigning a value to the property, on the left side of an assignment.
ctrl.ForeColor = vData
End Property
Public Property Get ForeColor() As Long
'used when retrieving value of a property, on the right side of an assignment.
ForeColor = ctrl.ForeColor
End Property
'-------------------------------------------------
Public Property Let BackColor(ByVal vData As Long)
'used when assigning a value to the property, on the left side of an assignment.
ctrl.BackColor = vData
End Property
Public Property Get BackColor() As Long
'used when retrieving value of a property, on the right side of an assignment.
BackColor = ctrl.BackColor
End Property
'-------------------------------------------------
'Property Top
Public Property Let Top(ByVal vData As Integer)
'used when assigning a value to the property, on the left side of an assignment.
ctrl.Top = vData
End Property
Public Property Get Top() As Integer
'used when retrieving value of a property, on the right side of an assignment.
Top = ctrl.Top
End Property
'-------------------------------------------------
'Property Left
Public Property Let Left(ByVal vData As Integer)
'used when assigning a value to the property, on the left side of an assignment.
ctrl.Left = vData
End Property
Public Property Get Left() As Integer
'used when retrieving value of a property, on the right side of an assignment.
Left = ctrl.Left
End Property
'-------------------------------------------------
'Property Width
Public Property Let Width(ByVal vData As Integer)
'used when assigning a value to the property, on the left side of an assignment.
ctrl.Width = vData
End Property
Public Property Get Width() As Integer
'used when retrieving value of a property, on the right side of an assignment.
Width = ctrl.Width
End Property
'-------------------------------------------------
'Property Height
Public Property Let Height(ByVal vData As Integer)
'used when assigning a value to the property, on the left side of an assignment.
ctrl.Height = vData
End Property
Public Property Get Height() As Integer
'used when retrieving value of a property, on the right side of an assignment.
Height = ctrl.Height
End Property
'-------------------------------------------------
[COLOR="Green"]'Events[/COLOR]
Private Sub ctrl_Click()
ctrl.ForeColor = vbBlue
MsgBox ctrl.Caption, , oldBtn
End Sub
'-------------------------------------------------
Private Sub ctrl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim objCtrl As MSForms.CommandButton
If oldBtn <> ctrl.Name Then
'--Reset
If oldBtn <> "" Then
If TypeName(ctrl.Parent) = "Worksheet" Then '// Parent is Worksheet
Set objCtrl = ctrl.Parent.OLEObjects(oldBtn).Object
Else '// Parent is Userform
Set objCtrl = ctrl.Parent.Controls(oldBtn)
End If
With objCtrl
.ForeColor = vbBlack
.BackColor = vb3DFace
.Font.Bold = False
End With
End If
'--Set New
oldBtn = ctrl.Name
ctrl.ForeColor = vbRed
ctrl.Font.Bold = True
ctrl.BackColor = vbYellow
End If
End Sub