ElektroStudios
ElektroStudios

Reputation: 20484

Event expected to raise but is not raised

I'm having problems trying to raise an event in this class

Private Sub Check()

    If Me.DaysLeft <= 0 Then
        RaiseEvent Expired(Me, New ExpiredEventArgs With {.DateExpired = TrialDateEnd})
        MsgBox("yes")
    Else
        RaiseEvent Active(Me, New ActiveEventArgs With {.DaysLeft = Me.DaysLeft})
        MsgBox("no")
    End If

End Sub

The msgbox is shown, but the event is not raised.

In my Form1 I have this:

Public Class Form1

    Private WithEvents _TrialExpiration As New TrialExpiration(4)

    Private Sub TrialExpiration_Active(sender As Object, e As TrialExpiration.ActiveEventArgs) _
    Handles _TrialExpiration.Active

        MsgBox(String.Format("You have {0} days remaining.", CStr(e.DaysLeft)))

    End Sub

    Private Sub TrialExpiration_Expired(sender As Object, e As TrialExpiration.ExpiredEventArgs) _
    Handles _TrialExpiration.Expired

        MsgBox(String.Format("Your copy of this software expired on {0}.", e.DateExpired.ToString))

    End Sub

End Class

And this is the TrialExpiration class:

#Region " TrialExpiration "

Public Class TrialExpiration

#Region " Variables "

    ''' <summary>
    ''' The date that the expiration started.
    ''' </summary>
    Private TrialDateStart As New Date(Nothing)

    ''' <summary>
    ''' The date that the expiration ends.
    ''' </summary>
    Private TrialDateEnd As New Date(Nothing)

    ''' <summary>
    ''' Expiration days.
    ''' </summary>
    Private TrialDays As Integer = 0

    ''' <summary>
    ''' Expiration days left.
    ''' </summary>
    Private DaysLeft As Integer = 0

#End Region

#Region " Events "

    ''' <summary>
    ''' Event raised when trial date is active.
    ''' </summary>
    Public Event Active As EventHandler(Of ActiveEventArgs)

    ''' <summary>
    ''' Event raised when trial date has expired.
    ''' </summary>
    Public Event Expired As EventHandler(Of ExpiredEventArgs)

    Public Class ActiveEventArgs : Inherits EventArgs
        ''' <summary>
        ''' Expiration Days left.
        ''' </summary>
        Public Property DaysLeft As Integer
    End Class

    Public Class ExpiredEventArgs : Inherits EventArgs
        ''' <summary>
        ''' Expiration Date.
        ''' </summary>
        Public Property DateExpired As Date
    End Class

#End Region

#Region " Constructor "

    ''' <summary>
    ''' Creates a new Trial Expiration.
    ''' </summary>
    ''' <param name="TrialDays">
    ''' Amount of days to expire.
    ''' </param>
    Public Sub New(ByVal TrialDays As Integer)
        Me.TrialDays = TrialDays
        SetTrialDates()
        GetDaysLeft()
    End Sub

#End Region

#Region " Public Methods "

    ''' <summary>
    ''' Resets the Trial Expiration.
    ''' </summary>
    Public Sub Reset()
        My.Settings.TrialDate = String.Empty
        My.Settings.Save()
        '  My.Settings.Reload()
    End Sub

#End Region

#Region " Private Methods "

    Private Sub SetTrialDates()

        ' If it's application first time run then set the initial date as Today.
        If String.IsNullOrEmpty(My.Settings.TrialDate) Then
            My.Settings.TrialDate = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(Today.ToString))
            My.Settings.Save()
            My.Settings.Reload()
        End If

        Try
            TrialDateStart = Date.Parse(System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(My.Settings.TrialDate)))
        Catch ex As FormatException
            ' Exception thrown if the user has corrupted the base64 string from the settings file.
            ' Then truncates the initial date to force trial expiration.
            TrialDateStart = Date.Parse("0001/01/01")
        End Try

        TrialDateEnd = TrialDateStart.AddDays(Me.TrialDays)

    End Sub

    Private Sub GetDaysLeft()

        Me.DaysLeft = (DateTime.Now.Subtract(Today) - DateTime.Now.Subtract(TrialDateEnd)).Days

        If Me.DaysLeft <= 0 _
        OrElse Today < TrialDateStart Then
            ' "OrElse Today < TrialDateStart" explanation:
            ' If the user has manipulated the date in the OS.
            RaiseEvent Expired(Me, New ExpiredEventArgs With {.DateExpired = TrialDateEnd})
        Else
            RaiseEvent Active(Me, New ActiveEventArgs With {.DaysLeft = Me.DaysLeft})
        End If

    End Sub

#End Region

End Class

#End Region

Upvotes: 0

Views: 216

Answers (1)

Teejay
Teejay

Reputation: 7501

Try adding the event handlers lately in the Form1.Load:

AddHandler _TrialExpiration.Active, AddressOf _TrialExpiration.Active

AddHandler _TrialExpiration.Expired, AddressOf _TrialExpiration.Expired

instead of Handles ... statements.

As a side note, I actually don't see any reason to use events in this case...

Upvotes: 2

Related Questions