Alan Rat
6
Q:

vb.net databinding not working on custom property

'The third thing to know is that in your class you must declare events for each property that will
'be used in data binding if you want change notifications to be sent from the object through the 
'data binding mechanism when the value of the property changes. However, there are rules you must
'follow when declaring these events that are not typical when otherwise using events in your
'program.

Public Class MyDataBindingClass
    'These variables must be initialized for databinding to work.
    'This is because manual initialization forces .Net to initialize the
    'variables before activating the databindings.
    'Otherwise, .Net tries to activate the databindings before automatic
    'initialization takes place and then the databindings fail.
    Private _ItemID As Long = 0
    Private _ItemName As String = "This is the current ItemName property value in the object."
    Private _ItemText As String = "This is the current ItemText property value in the object."

    'These events will allow the databinding mechanism in .Net to receive
    'notifications when the values change.
    'It is important that you name them exactly the same as the property name
    '(including capitalization) and add the word "Changed" to the end with a capital C.
    'And the event must be declared as "EventHandler"
    Public Event ItemIDChanged As EventHandler
    Public Event ItemNameChanged As EventHandler
    Public Event ItemTextChanged As EventHandler

    Public Property ItemName() As String
        Get
            Return Me._ItemName
        End Get
        Set(ByVal Value As String)
            Me._ItemName = Value
            'raise the event so databinding is notified
            RaiseEvent ItemNameChanged(Me, New EventArgs)
        End Set
    End Property

    Public Property ItemText() As String
        Get
            Return Me._ItemText
        End Get
        Set(ByVal Value As String)
            Me._ItemText = Value
            'raise the event so databinding is notified
            RaiseEvent ItemTextChanged(Me, New EventArgs)
        End Set
    End Property

    Public Property ItemID() As Long
        Get
            Return Me._ItemID
        End Get
        Set(ByVal Value As Long)
            Me._ItemID = Value
            'raise the event so databinding is notified
            RaiseEvent ItemIDChanged(Me, New EventArgs)
        End Set
    End Property
End Class
0

New to Communities?

Join the community