I’m having trouble using DataBindings in a Windows Form / User Control. For example:
Create a windows form, place a single text-box on it & place two buttons on it. The code behind the form is:
Imports System.ComponentModel
Public Class Form1
Implements System.ComponentModel.INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Private mSomeProperty As String
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.TextBox1.DataBindings.Add("text", Me, "SomeProperty", False, DataSourceUpdateMode.OnPropertyChanged)
End Sub
Public Property SomeProperty() As String
Get
Return mSomeProperty
End Get
Set(ByVal value As String)
mSomeProperty = value
NotifyPropertyChanged("SomeProperty")
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(Me.SomeProperty)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.SomeProperty = "Set in code - see it on the UI?"
End Sub
End Class
If you type something into the text box & press Button1, you’ll get a message box with the text of the property in it. If, however, you press Button2 (which sets the property in code) and then press Button1 again, you’ll see that while the property was indeed set in the code, the text box doesn’t reflect the change.
What am I missing, please!?
EDIT: Updated the code to provide the implementation of INotifyPropertyChanged. This now works as desired.
You need to implement the INotifyPropertyChanged interface.
There are lots of questions on this that may get you started if MSDN does not help you.