Link to home
Start Free TrialLog in
Avatar of jagguy
jagguyFlag for Australia

asked on

create my own events in vb.net

I am a bit confused about decalring your own events in vb.net. I understand why how and why you add events to dynamic controls like buttons using addhandler.

here is an example of creating my own class with an event but it fails to work.
i just want to be able to raise an event to see how this process can work
Public Class Form1
    Dim aa As New Thing("ssd", False)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a As Boolean
 
        AddHandler aa.OnFlagChanged, AddressOf HandleFlagChanged
 
    End Sub
 
    Private Sub HandleFlagChanged(ByVal sender As Object, ByVal e As EventArgs)
 
 
        MsgBox("_name")
 
 
    End Sub
 
End Class
 
 
Public Class Thing
    Private _flag As Boolean
    Private _name As String
    Public Event HandleFlagChanged As EventHandler
      
 
    Public Property Flag() As Boolean
        Get
            Return Me._flag
        End Get
        Set(ByVal value As Boolean)
            If Me._flag <> value Then
                Me._flag = value
            End If
        End Set
    End Property
 
    Public ReadOnly Property Name() As String
        Get
            Return Me._name
        End Get
    End Property
 
    Public Sub New(ByVal name As String, ByVal flag As Boolean)
        Me._name = name
        Me._flag = flag
    End Sub
    Public Sub OnFlagChanged(ByVal e As EventArgs)
        RaiseEvent HandleFlagChanged(Me, e)
    End Sub
 
End Class

Open in new window

Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Declare your event like this....

    Public Event FlagChanged(ByVal Value As Boolean)

Raise your event when the flag is changed, which is done in the Flag property....

    Public Property Flag() As Boolean
        Get
            Return Me._flag
        End Get
        Set(ByVal value As Boolean)
            If Me._flag <> value Then
                Me._flag = value
                RaiseEvent FlagChanged(value)
            End If
        End Set
    End Property

....then you'd handle the event with a routine like this....

    AddHandler aa.FlagChanged, AddressOf FlagChanged

    Private Sub FlagChanged(ByVal sender As Object, ByVal e As EventArgs)
        MsgBox("_name")
    End Sub

Wayne
you can do so using WithEvents keyword.
For details visit
http://www.devsource.com/c/a/Using-VS/Using-Class-Events-in-VBNet/
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jagguy

ASKER

ok i will look at it all