dotnet windows forms databinding:
Hello,
I'm currently reading on .NET data binding and I'm trying to do one of the
sample programs in the book. There is a text box bound to a property of an
array, and a previous/next button. The book mentions that if there are many
controls bound to the same element I do not have to take care of anything
and the values will be synchronized automatically. It does work, however I
need to go to the next row and then go back to see my change applied on all
the text boxes.
I also tried changing the array in code to see if the form would be updated
to reflect my changes, but it is not updated.
Finally, I also tried hooking the CurrencyManager class to see if I could
get the ItemChanged() event, but I'm only getting the CurrentChanged()
event.
I saw some post regarding the need to have a <PropertyName>Changed event,
but this did not help. Here's part of my code. Am I doing anything wrong? Is
this the expected behaviour? If so, how can I force the databinding to
resynchronize? And finally, when would the ItemChanged event fire?
Thanks!
Public Class TrainingForm
Inherits System.Windows.Forms.Form
Private m_Candidates() As Candidate = _
{New Candidate("Bill Gates", "305"), _
New Candidate("Steve Ballmer", "320")}
Private WithEvents m_CurrencyManager As CurrencyManager
Private Sub TrainingForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
CandidateName.DataBindings.Add("Text", m_Candidates,
"CandidateName")
ExamNumber.DataBindings.Add("Text", m_Candidates, "ExamNumber")
TextBox1.DataBindings.Add("Text", m_Candidates, "ExamNumber")
Label3.DataBindings.Add("Text", m_Candidates, "ExamNumber")
m_CurrencyManager = DirectCast(Me.BindingContext.Item(m_Candidates),
CurrencyManager)
End Sub
Private Sub NextButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles NextButton.Click
Me.BindingContext(m_Candidates).Position += 1
End Sub
Private Sub PreviousButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles PreviousButton.Click
Me.BindingContext(m_Candidates).Position -= 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
m_Candidates(0).ExamNumber = "123"
End Sub
Private Sub m_CurrencyManager_ItemChanged(ByVal sender As Object, ByVal
e As System.Windows.Forms.ItemChangedEventArgs) Handles
m_CurrencyManager.ItemChanged
MsgBox("Item Changed")
End Sub
Private Sub m_CurrencyManager_CurrentChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles m_CurrencyManager.CurrentChanged
MsgBox("Current changed")
End Sub
End Class
Public Class Candidate
Private m_ExamNumber As String
Private m_CandidateName As String
Public Event ExamNumberChanged As EventHandler
Public Event CandidateNameChanged As EventHandler
Public Property ExamNumber() As String
Get
Return m_ExamNumber
End Get
Set(ByVal Value As String)
m_ExamNumber = Value
RaiseEvent ExamNumberChanged(Me, New EventArgs)
End Set
End Property
Public Property CandidateName() As String
Get
Return m_CandidateName
End Get
Set(ByVal Value As String)
m_CandidateName = Value
RaiseEvent CandidateNameChanged(Me, New EventArgs)
End Set
End Property
Public Sub New(ByVal candidateName As String, Optional ByVal examNumber
As String = "")
m_CandidateName = candidateName
m_ExamNumber = examNumber
End Sub
End Class