You are on page 1of 3

Raising events Tutorial-1

First Step : ' Define Class with name "Person" ' Define event sub Public Class Person Private mName As String Private mBirthDate As String Private mID As String Public Event NameChanged(ByVal newName As String)

Sub New(ByVal _mName As String) mName = _mName End Sub

Public Overridable Property Name() As String Get Return mName End Get Set(ByVal value As String) mName = value RaiseEvent NameChanged(mName) End Set

End Property

End Class

Second Step : ' Insert New Button on your form and change its name to btnok

Third Step : ' double click on the button and write the next codes Public Class Form1 Private Sub OnNameChanged(ByVal newName As String) MsgBox("New name: " & newName) End Sub

The target sub which we will address the handler to it

Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click Dim p As Person = New Person("Salahudeen") AddHandler p.NameChanged, AddressOf OnNameChanged

p.Name = "khaled" End Sub End Class Result :

Add this statement to call the sub (OnNameChanged) in case of the person name has been changed

Event (NameChanged) is raised and it called the sub (OnNameChanged)

You might also like