BindingSource and CurrentChanged event

D

dvestal

The included program produces this output to the debug stream:

Changed!
Changed!
Changed!

Why does the event fire three times? Can I configure it such that it
only fires when the Current item is actually different?

My source program follows:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsApplication3
{
static class Program
{
[STAThread]
static void Main()
{
BindingSource src = new BindingSource();
BindingList<string> list = new BindingList<string>(
new string[]
{
"Odd"
}
);

src.CurrentChanged += new EventHandler(src_CurrentChanged);
src.DataSource = list;
}

static void src_CurrentChanged(object sender, EventArgs e)
{
Debug.WriteLine("Changed!");
}
}
}
 
D

dvestal

On 9/13/11 1:22 PM, (e-mail address removed) wrote:
Fortunately, it should not be difficult to treat consecutive invocations
of the event handler when the Current property has not really changed,
simply by keeping track of what the property was the last time the event
was raised and comparing it to the current Current value.

In any case, I do recommend submitting a bug on the Connect web site, to
at least see what Microsoft says about it.  Maybe they'd even offer a
better work-around than I have.

In the meantime, you can console yourself with the fact that this guy is
seeing the event being raised not three times, but FOUR:http://social.msdn.microsoft.com/forums/en-us/winformsdatacontrols/th...

And no one ever replied to his question at all.  :(

Pete

Thanks, Peter. When the property changes, I do some expensive
processing that's not _quite_ expensive enough to stick in a non-GUI
thread...unless it happens 3 times.

I worked around it by setting the datasource first, then subscribing
to the event. In the event handler and immediately after subscribing
to the event, I call a function that does the work, so I avoid the
initial glut of events.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top