How to add event handler into an event in VB.NET

+1 vote
asked Jun 9, 2013 in VB.NET by anonymous

I am implementing INotifyPropertyChanged interface in view model, so that I can raise PropertyChanged event whenever there is a change in any of the property. But I am new to VB.NET, so I do not know, how to add event handler in winforms to this PropertyChanged event to listen to that event. Here is the sample view model code I have. How write event handler in my form.

Imports System.ComponentModel

Public Class AccountInfoViewModel
    Implements INotifyPropertyChanged

    Private _accountNumber As Int64
    Public Property AccountNumber() As Int64
        Get
            Return _accountNumber
        End Get
        Set(ByVal value As Int64)
            _accountNumber = value
            PropertyChange("AccountNumber")
        End Set
    End Property

    Private _accountBalance As Double
    Public Property AccountBalance() As Double
        Get
            Return _accountBalance
        End Get
        Set(ByVal value As Double)
            _accountBalance = value
            PropertyChange("AccountBalance")
        End Set
    End Property

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Public Sub PropertyChange(_property As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(_property))
    End Sub

End Class
Share

1 Answer

+1 vote
answered Jun 10, 2013 by Bibek
selected Jun 10, 2013 by administrator
 
Best answer

Before you move further, If you are not understood the concept of Event and Delegate, I would suggest you to take a look at the below link. Even though the article with C#, it would help you to understand the process. Otherwise, please keep reading.

http://www.pinfaq.com/123/events-and-delegates-and-its-difference-dotnet-application

In VB.NET if you want to attach event handler for any event available in an object, then you may want to declare that object use "WithEvents" keyword. Once you do that, you shold be able to attach any event handler that has the same signature as the event is expecting.

Dim WithEvents _accountInfoViewModel As AccountInfoViewModel
 = New AccountInfoViewModel()

In your example INotifyPropertyChanged interface is using
PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)

So, you may want your event handler to have the same signature. Also, there are two ways you can attach event handlers in VB.NET.

Approach 1:

Public Sub OnPropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) **Handles _accountInfoViewModel.PropertyChanged**
'Here sender is ViewModel object.
'e is having proerpty changed event args which will have the name of the property which raised this event.
End Sub

Approach 2:
You can use AddHandler to attach event handler with an event. Call this below method from the constructor, so that the handlers will be attached at the right time.

Private Sub AttachHandlers()
        AddHandler _accountInfoViewModel.PropertyChanged, OnPropertyChanged
End Sub

Advantage of this second approach is, you can attach same event handler for same kind of events which are raised by multiple object. One good example for that would be, if you want to attach Validated event for all textbox controls on the page then you do something like below. So, that, the same event handler will be call if any of the textbox raise the Validated event.

Private Sub AttachHandlers()
        AddHandler txtFirstName.Validated, AddressOf Controls__Validated
        AddHandler txtLastName.Validated, AddressOf Controls__Validated
        AddHandler txtAddressLine1.Validated, AddressOf Controls__Validated
End Sub

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...