Databinding to a Checkbox

I have a simple form with 2 checkboxes which are Databound to an object, the properties of which are of type boolean.

The problem I have is :-

I am using the CheckChanged event. When I check the Box, the value of the UI control is correctly changed, but the value in the object does not reflect the change i.e. the Object seems to lag behind the form.

If I subsequently click on another control, and then check the values of the control and the object, they are now in sync.

Anyone got any suggestions

Cheers,
Paul





Answer this question

Databinding to a Checkbox

  • Gouri

    Could you explain how the checkbox is bound to the object

    In a databound or propertybound scenario, you should not have to handle the CheckChanged event... the binding manager should handle this for you.

    You might try using the MouseClick event and then test the state of the checkbox and act accordingly.

    Can you post the code that's in the CheckChanged event handler



  • Maxicus

    I've bound the checkbox to the object by using :-

    chkCollections.DataBindings.Add("Checked",OptrakExport,"ShowCollections")

    from within form Load, where chkCollections is the Checkbox on the form, OptrakExport is the object, and ShowCollections is the Property on the object.

    The code within CheckChanged was just there to check if the object value had changed, and if so then trigger a seperate piece of code. The problem I'm having is that when this event is fired, the screen variable is updated, but the property of the object is not.

    Thanks,
    Paul.

  • Freudi

    Thanks for the reply,

    you are right, it does appear to be that the event is being raised before the binding update has taken place. I've basically done as you suggested, and this gets me round the problem

    Many thanks,
    Paul

  • dx_user

    Ok,you may be facing an order of operations issue...

    I think (I'm not 100% sure though) that the CheckChanged event is being raised before the databinding update takes place. In other words, the event is being raised first and then after the event handling code executes the binding manager changes the ShowCollections value.

    The OptrakExport object appears to be a custom class, but if it's not you can still use the following work around by creating a custom class that inherits from OptrakExport and overriding the ShowCollections property. What you can do is add an event called ShowCollectionsChanged and raise it in the Set() method of the ShowCollections property after you've changed your internal value. Then your form can handle the ShowCollectionsChanged event to run that seperate piece of code you mentioned.

    HTH!



  • marijabo

    I'm having the same type of issue, however I either don't understand the answer above or it's not applying the same to me as the original poster. I think the latter is the case. I have attached the checkboxes to the binding source via the designer and are using them to calculate a score. Each one is worth X amount of points and I want to score the total score in the database. This is the code I am using.

    Private Sub Scoring_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ValidationCheckCheckBox.Click, TDCheckCheckBox.Click, ResolutionCheckCheckBox.Click, ExpectationsCheckCheckBox.Click

    'each time a check is changed, run through all checks and compute score and then store it in DB

    Dim intCalculatedScore As Integer

    If ValidationCheckCheckBox.Checked = True Then

    intCalculatedScore = 10

    Else

    intCalculatedScore = 0

    End If

    If TDCheckCheckBox.Checked = True Then

    intCalculatedScore = intCalculatedScore + 25

    End If

    If ResolutionCheckCheckBox.Checked = True Then

    intCalculatedScore = intCalculatedScore + 40

    End If

    If ExpectationsCheckCheckBox.Checked = True Then

    intCalculatedScore = intCalculatedScore + 25

    End If

    'here it prints the score as it should be, however after completed, the checkbox is returned to it's original state of check or unchecked

    Debug.Print(intCalculatedScore)

    Me.DatabaseDataSet.Tables("AgentData").Rows(AgentListBox.SelectedIndex).Item("Score") = intCalculatedScore

    AgentDataTableAdapter.Update(DatabaseDataSet.AgentData)

    AgentDataBindingSource.ResetItem(AgentListBox.SelectedIndex)

    End Sub

    So what happens is The box is at first checked, I click it and it flashes to unchecked, the score is calculated as if it is unchecked, but then the box is rechecked automatically. I'm sure im just missing something or have something in the wrong order


  • Databinding to a Checkbox