none
How to fire INotifyPropertyChanged event? RRS feed

  • Question

  • Hallo,

    I have a following class and i want to fire a sub EQ_PropertyChanged when my property is changed. I don't know how to implement it. Could anyone correct my code? Thanks.

    Public Sub EQ_PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)

    MessageBox.Show("FIRED")

    End Sub Public Class EQ_CLS Implements INotifyPropertyChanged Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Public Sub New(ByVal ValueCurrency As String, ByVal name As String) mValueCurrency = ValueCurrency mName = name End Sub Private mValueCurrency As String Public Property ValueCurrency() As String Get Return mValueCurrency End Get Set(ByVal ValueCr As String) mValueCurrency = ValueCr RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ValueCurrency")) End Set End Property Private mName As String Public Property Name() As String Get Return mName End Get Set(ByVal value As String) mName = value RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Name")) End Set End Property End Class





    Thursday, July 18, 2019 7:31 PM

Answers

  • Hi,

    I tried your code,it works well.

    Imports System.ComponentModel
    
    Public Class Form1
        Dim Eq As New EQ_CLS("1", "1")
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            AddHandler Eq.PropertyChanged, AddressOf Changed
        End Sub
        Private Sub Changed(sender As Object, e As PropertyChangedEventArgs)
            If e.PropertyName = "ValueCurrency" Then
                MessageBox.Show($"New value for ImageWidth is {Eq.ValueCurrency }")
            ElseIf e.PropertyName = "Name" Then
                MessageBox.Show($"Image name is now {Eq.Name }")
            End If
        End Sub
        Public Class EQ_CLS
    
            Implements INotifyPropertyChanged
            Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
            Public Sub New(ByVal ValueCurrency As String, ByVal name As String)
                mValueCurrency = ValueCurrency
                mName = name
            End Sub
    
    
            Private mValueCurrency As String
            Public Property ValueCurrency() As String
                Get
                    Return mValueCurrency
                End Get
                Set(ByVal ValueCr As String)
                    mValueCurrency = ValueCr
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ValueCurrency"))
                End Set
            End Property
    
            Private mName As String
            Public Property Name() As String
                Get
                    Return mName
                End Get
                Set(ByVal value As String)
                    mName = value
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Name"))
                End Set
            End Property
    
        End Class
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Eq.Name = "15"
            Eq.ValueCurrency = "11"
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Friday, July 19, 2019 5:47 AM
    Moderator

All replies

  • Here is one implementation

    Imports System.ComponentModel
    
    Public Class Form1
        Private ImageClass As New ImageClass
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler ImageClass.PropertyChanged, AddressOf Changed
            ImageClass.ImageWidth = 10
            ImageClass.ImageName = "Miata"
        End Sub
    
        Private Sub Changed(sender As Object, e As PropertyChangedEventArgs)
            If e.PropertyName = "ImageWidth" Then
                MessageBox.Show($"New value for ImageWidth is {ImageClass.ImageWidth}")
            ElseIf e.PropertyName = "ImageName" Then
                MessageBox.Show($"Image name is now {ImageClass.ImageName}")
            End If
        End Sub
    End Class
    Public Class ImageClass
        Implements INotifyPropertyChanged
    
        Public Event PropertyChanged As PropertyChangedEventHandler _
            Implements INotifyPropertyChanged.PropertyChanged
    
        Private _imageName As String
        Public Property ImageName() As String
            Get
                Return _imageName
            End Get
            Set(ByVal value As String)
                _imageName = value
                PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(NameOf(ImageName)))
            End Set
        End Property
        Private _imageWidth As Integer
        Public Property ImageWidth() As Integer
            Get
                Return _imageWidth
            End Get
            Set(ByVal value As Integer)
                _imageWidth = value
                PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(NameOf(ImageWidth)))
            End Set
        End Property
    End Class
    
    
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, July 18, 2019 9:43 PM
    Moderator
  • Here is one implementation

    Imports System.ComponentModel
    
    Public Class Form1
        Private ImageClass As New ImageClass
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler ImageClass.PropertyChanged, AddressOf Changed
            ImageClass.ImageWidth = 10
            ImageClass.ImageName = "Miata"
        End Sub
    
        Private Sub Changed(sender As Object, e As PropertyChangedEventArgs)
            If e.PropertyName = "ImageWidth" Then
                MessageBox.Show($"New value for ImageWidth is {ImageClass.ImageWidth}")
            ElseIf e.PropertyName = "ImageName" Then
                MessageBox.Show($"Image name is now {ImageClass.ImageName}")
            End If
        End Sub
    End Class
    Public Class ImageClass
        Implements INotifyPropertyChanged
    
        Public Event PropertyChanged As PropertyChangedEventHandler _
            Implements INotifyPropertyChanged.PropertyChanged
    
        Private _imageName As String
        Public Property ImageName() As String
            Get
                Return _imageName
            End Get
            Set(ByVal value As String)
                _imageName = value
                PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(NameOf(ImageName)))
            End Set
        End Property
        Private _imageWidth As Integer
        Public Property ImageWidth() As Integer
            Get
                Return _imageWidth
            End Get
            Set(ByVal value As Integer)
                _imageWidth = value
                PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(NameOf(ImageWidth)))
            End Set
        End Property
    End Class
    
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Hallo Karen, I am getting this error 

    Argument not specified for parameter "ValueCurrency' of Public Sub New(ValueCurrency as string, Name as string)

    Argument not specified for parameter "Name' of Public Sub New(ValueCurrency as string, Name as string)

    In my example i have additionally following code

    Public Sub New(ByVal ValueCurrency As String, ByVal name As String)
            mValueCurrency = ValueCurrency
            mName = name
    End Sub
    How do i supply these arguments? Thanks.


    Friday, July 19, 2019 5:19 AM
  • Hi,

    I tried your code,it works well.

    Imports System.ComponentModel
    
    Public Class Form1
        Dim Eq As New EQ_CLS("1", "1")
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            AddHandler Eq.PropertyChanged, AddressOf Changed
        End Sub
        Private Sub Changed(sender As Object, e As PropertyChangedEventArgs)
            If e.PropertyName = "ValueCurrency" Then
                MessageBox.Show($"New value for ImageWidth is {Eq.ValueCurrency }")
            ElseIf e.PropertyName = "Name" Then
                MessageBox.Show($"Image name is now {Eq.Name }")
            End If
        End Sub
        Public Class EQ_CLS
    
            Implements INotifyPropertyChanged
            Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
            Public Sub New(ByVal ValueCurrency As String, ByVal name As String)
                mValueCurrency = ValueCurrency
                mName = name
            End Sub
    
    
            Private mValueCurrency As String
            Public Property ValueCurrency() As String
                Get
                    Return mValueCurrency
                End Get
                Set(ByVal ValueCr As String)
                    mValueCurrency = ValueCr
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ValueCurrency"))
                End Set
            End Property
    
            Private mName As String
            Public Property Name() As String
                Get
                    Return mName
                End Get
                Set(ByVal value As String)
                    mName = value
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Name"))
                End Set
            End Property
    
        End Class
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Eq.Name = "15"
            Eq.ValueCurrency = "11"
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Friday, July 19, 2019 5:47 AM
    Moderator
  • VB is not a kind of JavaScript.

    Why would you set an event. Simply call the method which is handled by the event. 

    In older VB versions MethodName(nothing, nothing)


    Success
    Cor

    Friday, July 19, 2019 6:58 AM
  • VB is not a kind of JavaScript.

    Why would you set an event. Simply call the method which is handled by the event. 

    In older VB versions MethodName(nothing, nothing)


    Success
    Cor

    Thanks Cor, i will try to have a look at MethodName() 

            Private mName As String
            Public Property Name() As String
                Get
                    Return mName
                End Get
                Set(ByVal value As String)
                    mName = value
                    MethodName("" , ""))
                End Set
            End Property

    Is it what you suggest?
    Friday, July 19, 2019 7:34 AM
  • Here is one implementation

    Imports System.ComponentModel
    
    Public Class Form1
        Private ImageClass As New ImageClass
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler ImageClass.PropertyChanged, AddressOf Changed
            ImageClass.ImageWidth = 10
            ImageClass.ImageName = "Miata"
        End Sub
    
        Private Sub Changed(sender As Object, e As PropertyChangedEventArgs)
            If e.PropertyName = "ImageWidth" Then
                MessageBox.Show($"New value for ImageWidth is {ImageClass.ImageWidth}")
            ElseIf e.PropertyName = "ImageName" Then
                MessageBox.Show($"Image name is now {ImageClass.ImageName}")
            End If
        End Sub
    End Class
    Public Class ImageClass
        Implements INotifyPropertyChanged
    
        Public Event PropertyChanged As PropertyChangedEventHandler _
            Implements INotifyPropertyChanged.PropertyChanged
    
        Private _imageName As String
        Public Property ImageName() As String
            Get
                Return _imageName
            End Get
            Set(ByVal value As String)
                _imageName = value
                PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(NameOf(ImageName)))
            End Set
        End Property
        Private _imageWidth As Integer
        Public Property ImageWidth() As Integer
            Get
                Return _imageWidth
            End Get
            Set(ByVal value As Integer)
                _imageWidth = value
                PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(NameOf(ImageWidth)))
            End Set
        End Property
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Hallo Karen, I am getting this error 

    Argument not specified for parameter "ValueCurrency' of Public Sub New(ValueCurrency as string, Name as string)

    Argument not specified for parameter "Name' of Public Sub New(ValueCurrency as string, Name as string)

    In my example i have additionally following code

    Public Sub New(ByVal ValueCurrency As String, ByVal name As String)
            mValueCurrency = ValueCurrency
            mName = name
    End Sub
    How do i supply these arguments? Thanks.


    The code compiles just fine for me in VS2017 and when running produces two MessageBox's

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, July 19, 2019 10:13 AM
    Moderator