I Normally Use Withevents For Class Modules.
Say Your Writing a Windows Version of BlackJack. (For Some Reason) and You Write A Class module To Represent a Players Hand
Code:
'clsHand
Option Explicit
Dim lngScore As Long
Event Bust(Total As Long) 'Raise This Event If the Player goes over 21
Event Stick(Total As Long) ' Raise This Event if the Player Sticks
'When the Class Starts Deal a new Hand Between 2 and 21
Private Sub Class_Initialize()
lngScore = Fix(Rnd * 20) + 2
'Usually We'd Do Some more work Calulating the right odds
MsgBox "You Were Dealt " & lngScore
End Sub
Public Sub Twist()
Dim intCard As Integer
intCard = Fix(Rnd * 10) + 1
lngScore = lngScore + intCard
If lngScore > 21 Then
MsgBox "You Got a " & intCard & ". Bust!"
RaiseEvent Bust(lngScore)
Else
MsgBox "You Got a " & intCard & ", Giving you " & lngScore & "."
End If
End Sub
Public Sub Stick()
RaiseEvent Stick(lngScore)
End Sub
then you can yuse these events in your form by declaring an instance of the class WithEvents
Code:
Option Explicit
Dim WithEvents YourHand As clsHand
Private Sub cmdStick_Click()
YourHand.Stick
End Sub
Private Sub cmdTwist_Click()
YourHand.Twist
End Sub
Private Sub Form_Load()
Set YourHand = New clsHand
End Sub
Private Sub YourHand_Bust(Total As Long)
QueryReplay
End Sub
Private Sub QueryReplay()
If MsgBox("play again?", vbYesNo) = vbYes Then
Set YourHand = New clsHand
Else: Unload Me
End If
End Sub
Private Sub YourHand_Stick(Total As Long)
Dim lngDealer As Long
Dim strDealer As String
'Dealer Scores between 17 and 25
lngDealer = Fix(Rnd * 9) + 17
If lngDealer > 21 Then
strDealer = "Dealer is bust."
lngDealer = 0
Else
strDealer = "Dealer scores " & lngDealer & "."
End If
If Total > lngDealer Then
MsgBox strDealer & " You Win!"
Else
MsgBox strDealer & " You Loose!"
End If
QueryReplay
End Sub
obviously this isn't a very good game of blackjack, and there are far more usefull ways of using events in class modules. Your only limit is your imagination.