Question Databinding suppressing events

digitaldan3

Member
Joined
Jun 29, 2005
Messages
7
Programming Experience
3-5
I have 3 controls on a form. DatePicker1, DatePicker2, Button1

DatePicker1 and DatePicker2 are bound to a custom object MyObject(Date1 and Date2) which implements INotifyPropertyChanged.

DatePicker1 and DatePicker2 both subscribe to value changed events.

DatePicker1 ValueChanged event code says to add 2 days to MyObject.Date2

DatePicker2 ValueChanged event code says to add 2 days to MyObject.Date 1

Button1 Click event says to Add 3 days to MyOject.Date1.

This should cause a chain reaction which it does. I thought it would be an infinite loop. But it does stop after about 5 cycles without errors.

The desired behavior I would like is. Button Clicked MyObject.Date1 + 3 days... DatePicker1 gets updated. DatePicker1.ValueChanged does not fire and cause Date2 to change. I change date1 using the DatePicker it updates date2 but does not fire the value changed event of datepicker 2. I change the date in datepicker2 it updates date1 but again does not cause the value changed event of DatePicker1 to change.

I have tried suspendbinding, and RaiseListChangedEvents = false.
 
Add and remove event handlers dynamically:
VB.NET:
Private Sub Form1_Load(ByVal sender As Object, _
                       ByVal e As EventArgs) Handles MyBase.Load
    AddHandler Me.DateTimePicker1.ValueChanged, AddressOf DateTimePicker1_ValueChanged
    AddHandler Me.DateTimePicker2.ValueChanged, AddressOf DateTimePicker2_ValueChanged
End Sub

Private Sub DateTimePicker1_ValueChanged(ByVal sender As Object, _
                                         ByVal e As EventArgs)
    RemoveHandler Me.DateTimePicker2.ValueChanged, AddressOf DateTimePicker2_ValueChanged

    'Update DateTimePicker2

    AddHandler Me.DateTimePicker2.ValueChanged, AddressOf DateTimePicker2_ValueChanged
End Sub

Private Sub DateTimePicker2_ValueChanged(ByVal sender As Object, _
                                         ByVal e As EventArgs)
    RemoveHandler Me.DateTimePicker1.ValueChanged, AddressOf DateTimePicker1_ValueChanged

    'Update DateTimePicker1

    AddHandler Me.DateTimePicker1.ValueChanged, AddressOf DateTimePicker1_ValueChanged
End Sub
 
Back
Top