Option Explicit On

Option Strict

 

Imports System

Imports System.ComponentModel

Imports Microsoft.VisualBasic

 

Namespace EventSamplePublic

 

 

   

    '

    Public Class LaughEventArgs

        Inherits EventArgs

        Private _LaughPressed As Boolean

        Private nLaughs As Integer

 

        'Constructor.

        '

        Public Sub New(ByVal LaughPressed As Boolean, ByVal nLaughs As Integer)

            Me._LaughPressed = LaughPressed

            Me.nLaughs = nLaughs

        End Sub

 

      

        '

        Public ReadOnly Property NumRings() As Integer

            Get

                Return nLaughs

            End Get

        End Property

 

      

        '

        Public ReadOnly Property LaughPressed() As Boolean

            Get

                Return _LaughPressed

            End Get

        End Property

 

        ' The LaughType property that contains the Laugh sounds.

        '

        Public ReadOnly Property LaughType() As String

            Get

                If _LaughPressed Then

                    Return "He He He HHHe!!! Ha .. HaHaHaaa."

                Else

                    Return "Hiya Ha Ha!"

                End If

            End Get

        End Property

    End Class

 

 

    ' Delegate declaration.

    '

    Public Delegate Sub LaughEventHandler(ByVal sender As Object, _

    ByVal e As LaughEventArgs)

 

    ' The Laugh class that raises the Laugh event.

    '

    Public Class LaughingDoll

        Private _LaughPressed As Boolean = False

        Private nLaughs As Integer = 0

        Private stopFlag As Boolean = False

 

        ' The Stop property indicates whether the

        ' Laugh should be turned off.

        '

        Public Property [Stop]() As Boolean

            Get

                Return stopFlag

            End Get

            Set(ByVal value As Boolean)

                stopFlag = value

            End Set

        End Property

 

        ' The LaughPressed property indicates whether the pause

        ' button is pressed on the Laugh when the Laugh event is generated.

        '

        Public Property LaughPressed() As Boolean

            Get

                Return _LaughPressed

            End Get

            Set(ByVal value As Boolean)

                _LaughPressed = value

            End Set

        End Property

 

        ' The event member that is of type LaughEventHandler.

        '

        Public Event Laugh As LaughEventHandler

 

        ' The protected OnLaugh method raises the event by invoking

        ' the delegates. The sender is always this, the current instance

        ' of the class.

        '

        Protected Overridable Sub OnLaugh(ByVal e As LaughEventArgs)

            RaiseEvent Laugh(Me, e)

        End Sub

 

 

        ' This Laugh Doll does not have

        ' a user interface.

        ' To simulate the Laugh mechanism it has a loop

        ' that raises the Laugh event at every iteration

        ' with a time delay of 300 milliseconds,

        ' if pause is not pressed. If pause is pressed,

        ' the time delay is 1000 milliseconds.

        '

        Public Sub Start()

            Do

                nLaughs += 1

                If stopFlag Then

                    Exit Do

 

                Else

                    If _LaughPressed Then

                        System.Threading.Thread.Sleep(1000)

                        If (True) Then

                            Dim e As New LaughEventArgs(_LaughPressed, nLaughs)

                            OnLaugh(e)

                        End If

                    Else

                        System.Threading.Thread.Sleep(300)

                        Dim e As New LaughEventArgs(_LaughPressed, nLaughs)

                        OnLaugh(e)

                    End If

                End If

            Loop

        End Sub

    End Class

 

    ' The StartLaugh class has a method LaughRang that handles the

    ' Laugh event.

    '

    Public Class StartLaugh

        Public Sub LaughRang(ByVal sender As Object, ByVal e As LaughEventArgs)

 

            Console.WriteLine((e.LaughType + ControlChars.Cr))

 

            If Not e.LaughPressed Then

                If e.NumRings Mod 10 = 0 Then

                    Console.WriteLine(" Let Laugh Begin? Enter Y")

                    Console.WriteLine(" Press pause? Enter N")

                    Console.WriteLine(" Stop Laugh? Enter Q")

                    Dim input As String = Console.ReadLine()

 

                    If input.Equals("Y") Or input.Equals("y") Then

                        Return

 

                    Else

                        If input.Equals("N") Or input.Equals("n") Then

                            CType(sender, LaughingDoll).LaughPressed = True

                            Return

                        Else

                            CType(sender, LaughingDoll).Stop = True

                            Return

                        End If

                    End If

                End If

            Else

                Console.WriteLine(" Let Laugh Start? Enter Y")

                Console.WriteLine(" Stop Laugh? Enter Q")

                Dim input As String = Console.ReadLine()

                If input.Equals("Y") Or input.Equals("y") Then

                    Return

                Else

                    CType(sender, LaughingDoll).Stop = True

                    Return

                End If

            End If

        End Sub

    End Class

 

    ' The driver class that hooks up the event handling method of

    ' StartLaugh to the Laugh event of an Laugh object using a delegate.

    ' In a forms-based application, the driver class is the

    ' form.

    '

    Public Class LaughDriver

        Public Sub startLaugh()

            ' Instantiates the event receiver.

            Dim w As New StartLaugh()

 

            ' Instantiates the event source.

            Dim Doll As New LaughingDoll()

 

            ' Wires the LaughRang method to the Laugh event.

            AddHandler Doll.Laugh, AddressOf w.LaughRang

 

            Doll.Start()

        End Sub

    End Class

 

End Namespace