0% found this document useful (0 votes)
11 views3 pages

Raising Events in VB.NET Tutorial

The document provides instructions for raising an event in Visual Basic. It involves: 1) Defining a Person class with a Name property and NameChanged event. 2) Creating a form with a button and handler sub to display changed names. 3) Instantiating a Person object, adding a handler for its NameChanged event, and changing its name to trigger the event.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Raising Events in VB.NET Tutorial

The document provides instructions for raising an event in Visual Basic. It involves: 1) Defining a Person class with a Name property and NameChanged event. 2) Creating a form with a button and handler sub to display changed names. 3) Instantiating a Person object, adding a handler for its NameChanged event, and changing its name to trigger the event.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 [Link] Dim p As Person = New Person("Salahudeen") AddHandler [Link], AddressOf OnNameChanged

[Link] = "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