我有这个用户表单(图 1),我正在尝试通过类模块应用一些自定义。因此,我的第一个目标是在单击时修改标签格式(图 2)。到目前为止一切顺利,我已经通过类模块“cLabels”完成了这项工作。现在,我的第二个目标是(这是我遇到的问题)为上述标签应用其他颜色。关键是,我不知道如何做到这一点。
我尝试创建其他名为“cUserForm”的类模块,但我不知道如何将修改后的标签传递给 cUserForm 类模块并使用它的 MouseMove 事件。我知道我可以使用 MouseMove 事件通过标准 UserForm 模块应用修改,但问题是,我不希望在我的 UserForm 模块中有任何类似的代码,我希望类模块做“脏”工作。伙计们有什么想法我该如何规避这个问题?
附加信息(但对解决问题并不重要):我的最终目标是制作像这样的“按钮” https://drive.google.com/file/d/1ev_LNgxPqjMv0dtzlF7GSo7SOq0wDbR2/view?usp=sharing一些效果,例如 MouseHover , TabPress 等等。VBA 按钮非常难看。只是为了记录,我已经在一个标准的 UserForm 模块中完成了所有这些(如果有人想让工作簿看到我在说什么,我有它),但最终结果只是一团糟,这么多代码(这只是修改用户窗体外观的代码,想象一下当我放一些代码来做某些动作时,天哪)。
这是我到目前为止所拥有的:
用户窗体模块
Option Explicit
Private ObjLabel As cLabels
Private ObjUserForm As cUserForm
Private Sub UserForm_Initialize()
Set ObjLabel = New cLabels
ObjLabel.CallClasse Me
Set ObjUserForm = New cUserForm
Set ObjUserForm.UserFormValue = Me
End Sub
c标签
Option Explicit
'## Events/Variables/Collections
Private WithEvents clsLabel As MSForms.Label
Private ClasseObject As cLabels
Private LabelCollection As New Collection
'## Properties
Public Property Get ActiveLabel() As MSForms.Label
Set ActiveLabel = clsLabel
End Property
Public Property Set ActiveLabel(Value As MSForms.Label)
Set clsLabel = Value
End Property
'## Procedures/Methods
Private Sub clsLabel_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
LabelHovered
End Sub
Public Sub CallClasse(MainObject As MSForms.UserForm)
Dim ctrl As MSForms.Control
For Each ctrl In MainObject.Controls
If TypeOf ctrl Is MSForms.Label Then
Set ClasseObject = New cLabels
Set ClasseObject.ActiveLabel = ctrl
LabelCollection.Add ClasseObject
End If
Next ctrl
End Sub
Private Sub LabelHovered()
ActiveLabel.BackColor = vbYellow
End Sub
cUserForm
Option Explicit
'## Events/Variables/Collections
Private WithEvents clsUserForm As MSForms.UserForm
Private mActiveLabel As MSForms.Label
Private ObjLabel As New cLabels
'## Properties
Public Property Get UserFormValue() As MSForms.UserForm
Set UserFormValue = clsUserForm
End Property
Public Property Set UserFormValue(Value As MSForms.UserForm)
Set clsUserForm = Value
End Property
Public Property Get ActiveLabel() As MSForms.Label
Set ActiveLabel = mActiveLabel
End Property
Public Property Set ActiveLabel(Value As MSForms.Label)
Set mActiveLabel = Value
End Property
'## Procedures
Private Sub clsUserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'MsgBox ObjLabel.ActiveLabel.BackColor 'Got an error
End Sub
工作簿: https ://drive.google.com/file/d/1cLG4pLmC-jDaysjd_dK0EFuJ_LqYqJ-u/view?usp=sharing