用raiseeventOption ExplicitPrivate WithEvents mText As TimerStatePrivate Sub Command1_Click()
Text1.Text = "From Now"
Text1.Refresh
Text2.Text = "0"
Text2.Refresh
Call mText.TimerTask(9.84)
End SubPrivate Sub Form_Load()
Command1.Caption = "Click to Start Timer"
Text1.Text = ""
Text2.Text = ""
Label1.Caption = "The fastest 100 meters ever run took this long:"
Set mText = New TimerState
End SubPrivate Sub mText_ChangeText()
Text1.Text = "Until Now"
Text2.Text = "9.84"
End SubPrivate Sub mText_UpdateTime(ByVal dblJump As Double)
Text2.Text = Str(Format(dblJump, "0"))
DoEvents
End SubThe remaining code is in a class module named TimerState. Included among the commands in this module are the Raise Event statements.Option Explicit
Public Event UpdateTime(ByVal dblJump As Double)
Public Event ChangeText()Public Sub TimerTask(ByVal Duration As Double)
Dim dblStart As Double
Dim dblSecond As Double
Dim dblSoFar As Double
dblStart = Timer
dblSoFar = dblStart
Do While Timer < dblStart + Duration
If Timer - dblSoFar >= 1 Then
dblSoFar = dblSoFar + 1
RaiseEvent UpdateTime(Timer - dblStart)
End If
Loop
RaiseEvent ChangeText
End Sub
Text1.Text = "From Now"
Text1.Refresh
Text2.Text = "0"
Text2.Refresh
Call mText.TimerTask(9.84)
End SubPrivate Sub Form_Load()
Command1.Caption = "Click to Start Timer"
Text1.Text = ""
Text2.Text = ""
Label1.Caption = "The fastest 100 meters ever run took this long:"
Set mText = New TimerState
End SubPrivate Sub mText_ChangeText()
Text1.Text = "Until Now"
Text2.Text = "9.84"
End SubPrivate Sub mText_UpdateTime(ByVal dblJump As Double)
Text2.Text = Str(Format(dblJump, "0"))
DoEvents
End SubThe remaining code is in a class module named TimerState. Included among the commands in this module are the Raise Event statements.Option Explicit
Public Event UpdateTime(ByVal dblJump As Double)
Public Event ChangeText()Public Sub TimerTask(ByVal Duration As Double)
Dim dblStart As Double
Dim dblSecond As Double
Dim dblSoFar As Double
dblStart = Timer
dblSoFar = dblStart
Do While Timer < dblStart + Duration
If Timer - dblSoFar >= 1 Then
dblSoFar = dblSoFar + 1
RaiseEvent UpdateTime(Timer - dblStart)
End If
Loop
RaiseEvent ChangeText
End Sub
定义好事件后,在你的form中你认为该产生该事件的地方用RaiseEvent ModeChange(mode)触发在其他模块中采用如下方式定义form就可以捕获事件了:dim withevents form as frmYourFramprivate sub form_modeChange(mode as Integer)
end sub
mk:@MSITStore:D:\Program%20Files\Microsoft%20Visual%20Studio\MSDN98\98VSa\1033\vbcon98.chm::/Html/vbconaddingeventtoform.htm
看 Adding an Event to a Form