Results 1 to 3 of 3

Thread: It is time for an EVENT.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    I usually use WithEvents to borrow other controls' events.

    What are some of the other uses that you usually do with WithEvents? If you have code examples, please provided.

    Thanks!
    Chemically Formulated As:
    Dr. Nitro

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633

    Thumbs up

    Like always thanks Sam!
    Chemically Formulated As:
    Dr. Nitro

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width