Declare and Raise an event in VB.NET

In this article you will learn how to Declare and Raise an event in VB.NET.
  • 2917

How to Declare  an event     

You have to use a keyword for declare an event that is "Event" . This event shows the progress of the task.

Code of declaration

Public Event ProgressDone(ByVal Progress As Single,

                         ByRef Cancel As Boolean)

The cancel argument in ByRef is used to cancel the method that raised the event, and you have to set is True for that.


How to Raise an event

First you have to Import a statement to the top of class for using timer property in Widget class.

Imports Microsoft.VisualBasic.DateAndTime

The Progress event is raised by the Task method which is present in the Widget class. Task takes two arguments: the length of time the method, and the minimum time interval before Task pauses to raise the ProgressDone event.

Write this code in widget class


Public Sub Task(ByVal Duration As Single,

                    ByVal MinimumInterval As Single)

    Dim Threshold As Single

    Dim Start As Single

    Dim Cancel As Boolean



    Start = CSng(Timer)

    Threshold = MinimumInterval


    Do While CSng(Timer) < (Start + Duration)

        If CSng(Timer) > (Start + Threshold) Then

            RaiseEvent ProgressDone(

            Threshold / Duration, Cancel)

            If Cancel Then Exit Sub

            Threshold = Threshold + MinimumInterval

        End If


End Sub



More Articles

© 2020 DotNetHeaven. All rights reserved.