Link to home
Start Free TrialLog in
Avatar of RyanAndres
RyanAndresFlag for United States of America

asked on

Fire one event when a BindingSource's Current object's property changes.

I have a BindingSource (employeeBindingSource) who's DataSource is an object array (Employee[]).

It is bound to a BindingNavigator, a ToolStripComboBox, and several Common Windows Form Controls.

GOAL: Fire only one event when the user changes a bound value. Such as text in a TextBox, state of a CheckBox, value of a DateTimePicker, etc.

PROBLEM: I am handling the CurrentItemChanged event but whenever the user changes a text value, this event gets fired twice on validation. According to MSDN, this is expected since it is fired once for the property change and again for the CurrentChanged.

I tried to handle the ListChanged event and checked the ListChangedType for ItemChanged. I got the same results. The event was fired twice!

QUESTION:
So, which event(s) will allow me to detect changes in a BindingSource's Current properties just once per change?
employeeBindingSource.DataSource = _hrClient.GetEmployees(null, null, true, false);
employeeBindingSource.CurrentItemChanged += employeeBindingSource_CurrentItemChanged;
employeeBindingSource.ListChanged += employeeBindingSource_ListChanged;
bindingNavigator.BindingSource = employeeBindingSource;
 
......
 
// The following methods allowed me to see the events fired multiple times
 
private void employeeBindingSource_CurrentItemChanged(object sender, EventArgs e)
{
    string text = lstPastStudiesEmp.Items.Count + " CurrentItemChanged";
    listbox1.Items.Insert(0, text);
}
 
private void employeeBindingSource_ListChanged(object sender, ListChangedEventArgs e)
{
    string text = lstPastStudiesEmp.Items.Count + " " + e.ListChangedType;
    lstPastStudiesEmp.Items.Insert(0, text);
}

Open in new window

Avatar of topdog770
topdog770
Flag of United States of America image

Have you tried just using CurrentChanged ?  It should fire anytime the data changes, but not until then.
 
 
 
ASKER CERTIFIED SOLUTION
Avatar of RyanAndres
RyanAndres
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial