窗體也是類,所以類中響應窗體自定義事件也是可以的...
不過樓主的程式碼邏輯有問題,程式碼也有問題,類要建立例項才行:
"----------------------- "窗體程式碼:-----------------------
Option Explicit
"宣告事件
Public Event LoginRequest(ByVal userid As String)
"宣告啟動事件的過程
Public Sub StartTest()
RaiseEvent LoginRequest("123456")
End Sub
"例項化類
Private Sub Command1_Click()
Dim cls As Class1
Set cls = New Class1
"-----------------------------------------------------------
"------------------------ "類程式碼 --------------------------
Public WithEvents mLogin As Form1
"響應窗體Form1的事件
Private Sub mLogin_LoginRequest(ByVal userid As String)
mLogin.Text1.Text = userid
"例項化mLogin,並執行mLogin.StartTest方法
Private Sub Class_Initialize()
Set mLogin = Form1 "指向Form1,不能用new...
mLogin.StartTest
窗體也是類,所以類中響應窗體自定義事件也是可以的...
不過樓主的程式碼邏輯有問題,程式碼也有問題,類要建立例項才行:
"----------------------- "窗體程式碼:-----------------------
Option Explicit
"宣告事件
Public Event LoginRequest(ByVal userid As String)
"宣告啟動事件的過程
Public Sub StartTest()
RaiseEvent LoginRequest("123456")
End Sub
"例項化類
Private Sub Command1_Click()
Dim cls As Class1
Set cls = New Class1
End Sub
"-----------------------------------------------------------
"------------------------ "類程式碼 --------------------------
Option Explicit
Public WithEvents mLogin As Form1
"響應窗體Form1的事件
Private Sub mLogin_LoginRequest(ByVal userid As String)
mLogin.Text1.Text = userid
End Sub
"例項化mLogin,並執行mLogin.StartTest方法
Private Sub Class_Initialize()
Set mLogin = Form1 "指向Form1,不能用new...
mLogin.StartTest
End Sub
"-----------------------------------------------------------