事件预计会提高但不会提高

时间:2013-11-21 08:29:38

标签: .net vb.net events

我在尝试在这个课程中提出一个事件时遇到了问题

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

显示了msgbox,但未引发该事件。

在我的Form1中我有这个:

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

这是TrialExpiration类:

#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

1 个答案:

答案 0 :(得分:2)

尝试在Form1.Load

中添加事件处理程序
AddHandler _TrialExpiration.Active, AddressOf _TrialExpiration.Active

AddHandler _TrialExpiration.Expired, AddressOf _TrialExpiration.Expired

而不是Handles ...语句。

作为旁注,我实际上看不出在这种情况下使用事件的任何理由......