Winform CheckBox databinding issue

Winform CheckBox databinding issue

Old forum URL:

bala345 posted on Saturday, September 22, 2012

When a bool property in my BO is bound to a CheckBox in a form, while  after clicking the checkbox I have to change the focus by clicking somewhere else. Only then the BO gets updated. 

To overcome this I have used the following remedy which may not be a better solution but it solves it. If anybody know a better one please suggest me. 

Private Sub DiscountCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DiscountCheckBox.CheckedChanged

        If DiscountCheckBox.Focused Then

            Dim localBO As BO = CType(BOBindingSource.Current,  BO)

            If localBO IsNot Nothing Then

                localBO.DiscountAllowed  =   DiscountCheckBox.Checked


                 DiscountCheckBox .Update()

            End If

        End If

    End Sub




JonnyBee replied on Saturday, September 22, 2012

Simplest solution is to change the DataBinding UpdateMode to OnPropertyChanged. 

The default mode is OnValidation, ie: when another control that has CausesValidation = true gets focus.

I also recommend the DataBinding FAQ to learn more about Windows Forms DataBinding.

bala345 replied on Monday, September 24, 2012

Thanks a million - 

As per your post I inserted this in the load event of the Usercontrol  -

" MyCheckBox.DataBindings("Checked").DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged "

It worked perfectly.  

Thank you once again. 

Copyright (c) Marimer LLC