How to bind form label or text box to class variable

mysiar

Member
Joined
Mar 10, 2009
Messages
7
Programming Experience
Beginner
Hi
I have a problem to solve and not quite sure if the way I think is correct.

I have a main form class MainForm
the class has got:
a variable call Value
and a label called LabelValue

Now I would like to automatically update LabelValue every time the Value changes his value

Piotr
 
The immediate solution is simply to write a Property, where in setter you also set the controls property:
VB.NET:
Private newPropertyValue As String
Public Property NewProperty() As String
    Get
        Return newPropertyValue
    End Get
    Set(ByVal value As String)
        newPropertyValue = value
        [COLOR="Green"]Me.Label1.Text = value[/COLOR]
    End Set
End Property
To add properties to a class (yes, a form is a class), write "prop" and press Tab key twice, IDE then inserts the skeleton you see above, fill in the 'blanks'.

The solution above is fair enough to achieve the goal for this Label update in a form. A better more generic solution is to implement INotifyPropertyChanged and add a two-way databinding to the control property. This adds a few lines of code, but scales up really well. Example:
VB.NET:
Public Class Form1
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Protected Overridable Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
        RaiseEvent PropertyChanged(Me, e)
    End Sub

    Private newPropertyValue As String
    Public Property NewProperty() As String
        Get
            Return newPropertyValue
        End Get
        Set(ByVal value As String)
            If newPropertyValue <> value Then
                newPropertyValue = value
                [COLOR="Green"]OnPropertyChanged(New PropertyChangedEventArgs("NewProperty"))[/COLOR]
            End If
        End Set
    End Property

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Label1.DataBindings.Add("Text", Me, "NewProperty")
    End Sub

End Class
Here you see a PropertyChanged event is added (including the OnPropertyChanged method), property setter calls this to notify it has changed. Also notice that here it is a greater importance that the value has actually changed, since there could be other listeners to the event that is only interested in knowing when the property value has changed, and not whether it has been set to same value many times in a row. If adding more properties you can now see that you only need to add one line of code to add PropertyChanged notification for these also.

In forms Load event handler the controls DataBindings is added, this is the same you can do in Designer for DataBindings to data sources and bindings for ApplicationSettings, only Designer doesn't allow to browse to a custom object like here. Label1.DataBindings.Add ("Text", Me, "NewProperty") means Text property of the label is bound to NewProperty of the form class instance.

Note that the Bindings default DataSourceUpdateMode is OnValidation, a Label doesn't validate so to enable pushing value changes from control property to the object property you can set update mode to OnPropertyChanged instead.
 
Back
Top