Binding and lostfocus

  • Thread starter Thread starter Grim Rob
  • Start date Start date
G

Grim Rob

I am using .NET Framework v2. I am binding properties to controls
using complex binding. However until you tab off each control to lose
focus the object being bound to is not updated. To get round this I am
using SendKeys to tab on and off the control. This works fine but you
have to put the sendkeys code in every event handler being bound to
which is horrible. Is there a better way?

Here are some code fragments.

Protected m_BindingSource As BindingSource = New BindingSource

UpdateBinding(chkWeek1Month1AtLeaving, "Checked",
m_BindingSource, "Week1Month1IndicatorAtLeaving")

Protected Sub updateBinding(ByVal c As Control, ByVal prop As
String, ByVal BindingSource As BindingSource, ByVal dataMember As
String)
If c.DataBindings(prop) IsNot Nothing Then
c.DataBindings.Remove(c.DataBindings(prop))
c.DataBindings.Add(prop, BindingSource.DataSource,
dataMember)
End Sub


Private Sub chkWeek1Month1AtLeaving_CheckedChanged(ByVal sender
As Object, ByVal e As System.EventArgs) Handles
chkWeek1Month1AtLeaving.CheckedChanged
SendKeys.Send(vbTab + "+" + vbTab)
End Sub
 
I have found the answer myself:

Protected Sub updateBinding(ByVal c As Control, ByVal prop As
String, ByVal BindingSource As BindingSource, ByVal dataMember As
String) ' TODO SLAP IN BASE
If c.DataBindings(prop) IsNot Nothing Then
c.DataBindings.Remove(c.DataBindings(prop))
Dim b As Binding = New Binding(prop,
BindingSource.DataSource, dataMember)
b.DataSourceUpdateMode =
DataSourceUpdateMode.OnPropertyChanged
c.DataBindings.Add(b)
End Sub
 
Back
Top