例如:
Form1(Main Form) :
---------------------------------------------------------------------------
Private WithEvents AAA As ClsABCD
...
...
Private Sub AAA_BackStruct(ByVal PNum As Integer, ByVal FNum As Integer)
MsgBox "No !"
End Sub
---------------------------------------------------------------------------
Form2 :
---------------------------------------------------------------------------
Private WithEvents BBB As ClsABCD
...
...
Private Sub BBB_BackStruct(ByVal PNum As Integer, ByVal FNum As Integer)
MsgBox "OK !"
End Sub
---------------------------------------------------------------------------
Class ClsABCD :
---------------------------------------------------------------------------
Public Event BackStruct(ByVal iPNum As Integer, ByVal iFNum As Integer)
...
...
...
...
RaiseEvent BackStruct(1, FlowNum)'但是每到这行就会跳到Main Form里的SUB中
...
...
---------------------------------------------------------------------------
不知道我是漏了什么?我两边都有类事件返回的需求.
第一个窗体,是按你这个猜想去的。
Public WithEvents AAA As ClsABCD
然后
set AAA = new ClsABCD
第二个窗体,
set BBB = ClsABCE
这样二个窗体引用同一个类。类里的事件,两个窗体同时显示,我测试时,是开了二个窗体,关掉一个窗体我没去测试。
-----------------------
如果
第二个窗体
set BBB = new ClsABCD
那么事件响应就是各自的窗体响应对应的类。
---------------------
----Form1------
Public WithEvents AAA As ClsABCD
Private Sub AAA_BackStruct(ByVal PNum As Integer, ByVal FNum As Integer)
MsgBox "No 1"
End Sub
Private Sub Command1_Click()
Call AAA.ev
End Sub
Private Sub Form_Load()
Set AAA = New ClsABCD
Form2.Show
End Sub
----Form2------
Private WithEvents BBB As ClsABCD
Private Sub BBB_BackStruct(ByVal PNum As Integer, ByVal FNum As Integer)
MsgBox "No 2"
End Sub
Private Sub Command1_Click()
Call BBB.ev '手动触发事件
End Sub
Private Sub Form_Load()
Set BBB = Form1.AAA
'Set BBB = New ClsABCD
End Sub
----ClsABCD--------
Public Event BackStruct(ByVal iPNum As Integer, ByVal iFNum As Integer)
Const Flownum = 2
Public Sub ev()
RaiseEvent BackStruct(1, Flownum)
End Sub
在Form2 :
1. 写成 set BBB = ClsABCE 光源码编译就过不了.
2. 写成 Set BBB = Form1.AAA 源码编译就过了.
--------------------------------------------------------------------
直接试了你的源码发现你设计的功能好像跟我要的刚好相反.
不过也差不多,也因此找到解决方向了.
--------------------------------------------------------------------
Form1:--------------------------------------------------------------
Public WithEvents AAA As ClsABCD
Private Sub AAA_Back()
Form1.Hide
Form2.Show
End Sub
Private Sub AAA_BackStruct(ByVal iCount As Integer)
ProgressBar1.Value = iCount
End Sub
Private Sub Command1_Click()
With AAA
.nCount = 100
.Run
End With
End Sub
Private Sub Form_Load()
Set AAA = New ClsABCD
ProgressBar1.Value = 0
Form2.Show
Form2.Hide
End Sub
Form2:--------------------------------------------------------------
Private WithEvents BBB As ClsABCD
Private Sub BBB_BackStruct(ByVal iCount As Integer)
ProgressBar1.Value = iCount
End Sub
Private Sub Form_Load()
Set BBB = Form1.AAA
ProgressBar1.Value = 0
End Sub
ClsABCD:--------------------------------------------------------------
Private mvarnCount As Integer
Public Event BackStruct(ByVal iCount As Integer)
Public Event Back()
Public Property Let nCount(ByVal vData As Integer)
mvarnCount = vData
End Property
Public Sub Run()
Dim i As Integer, j As Integer
For i = 0 To mvarnCount
RaiseEvent BackStruct(i)
Next i
RaiseEvent Back
For j = 0 To mvarnCount
RaiseEvent BackStruct(j)
Next j
End Sub