我有一个UserForm(frmProgress),它应该用标签显示进程的进度。在类模块(clsProgressForm)中,我按如下方式创建UserForm:
Option Explicit
Public Event UpdateProgress(ByVal str As String)
Private Sub Class_Initialize()
Dim frm As frmProgress
Set frm = New frmProgress
Load frm
frm.Show vbModeless
RaiseEvent UpdateProgress("test")
End Sub
UserForm包含以下代码:
Option Explicit
Private WithEvents frm As MSForms.UserForm
Private Sub frm_UpdateProgress(ByVal str As String)
lblUpdate.Caption = str
End Sub
但是从不调用UserForm上的frm_UpdateProgress事件。如何使用事件更新UserForm?
答案 0 :(得分:0)
如果你想让这个具体的例子以你的思维方式运作,你需要改变一些事情:
frmProgress:
Option Explicit
Public WithEvents oProgressFormClassInstance As clsProgressForm
'The WithEvents here implies that whenever the specific object referenced by this variable
'throws an event, it will trigger the event handler for that event in this class (form)
Private Sub oProgressFormClassInstance_UpdateProgress(ByVal str As String)
lblUpdate.Caption = str
End Sub
clsProgressForm:
Option Explicit
Public Event UpdateProgress(ByVal str As String)
Private Sub Class_Initialize()
Dim frm As frmProgress
Set frm = New frmProgress
Set frm.oProgressFormClassInstance = Me
Load frm
frm.Show vbModeless
RaiseEvent UpdateProgress("test")
End Sub
测试模块mdlTest:
Public Sub Test()
Dim oProgressForm As clsProgressForm
Set oProgressForm = New clsProgressForm
End Sub
运行Test
函数以查看表单和类的运行情况。如果你仍然不确定事件是如何运作的,请告诉我,我可以扩大答案。