Search code examples
vb.netvbaole

How to listen variable property changes with external program


I'm having a problem with my program that at its core uses a class variable to connect, control and extract information from CAD/CAM software called PowerShape.

What I'm trying to do is listen to this class variable to detect changes in its properties which happen if you do something inside Powershape. These would include the active window or model changing inside Powershape. The class variable is updating when changes are made, but I can't figure out how to detect it.

When class variable is declared it connects to Powershape and then you can access its properties:

Dim powershapeRoot As New PSAutomation(Delcam.ProductInterface.InstanceReuse.UseExistingInstance)
Dim PSmodelname = PowershapeRoot.activemodel.name

Now I would like to listen to the variable property "PowershapeRoot.activemodel.name" and see if it changes

How to do this?


Solution

  • To detect changes in the properties, you could use the INotifyPropertyChanged Interface.

    You will find here the documentation from the MSDN.

    In the setter of your property, you will need to include the code to raise the event. You can find an example below in VB.NET :

    Public Class Demo Implements INotifyPropertyChanged
    
        Private nameValue As String = String.Empty
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
        Private Sub NotifyPropertyChanged(ByVal info As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
        End Sub
    
        Public Property name() As String
            Get
                Return Me.nameValue
        End Get
    
        'Raise the event in the setter
        Set(ByVal value As String)
            If Not (value = nameValue) Then
                Me.nameValue = value
                NotifyPropertyChanged("name")
            End If
        End Set
        End Property
    End Class