VB.NET Detected user changes in a Bound Combobox

How do I detect user changes in a Bound Combobox without having the event fire during loading and bindingsource changes?  The problem is that I only want one a specific event to fire when a user clicks or uses the keyboard to make a change.  The problem with the SelectedIndexChanged event is that it fires all the time, including when the BindingSource1 data is loaded/changed, and when the Bindinsource2.position changes.  Any relatively easily solution without a complicated set of flags, etc?

For a combobox named [b]cboStuff[/b]:
cboStuff.Datasource = BindingSource1
DisplayMember = WidgetDescription
ValueMember = WidgetID

[b]SelectedValue = BindingSource2 - WidgetID[/b]

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try make a public variable like a Boolean and place it into the SelectedIndexChanged event to verify if it's in Binding stage, if yes, then doing the escape else continue to run your codes.


Public isBinding As Boolean = Flase

Open in new window

isBinding = True
cboStuff.Datasource = BindingSource1
DisplayMember = WidgetDescription
ValueMember = WidgetID
isBinding = False

Open in new window

in SelectedIndexChanged , just check:

If isBinding Then Exit Sub
'your other codes here

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
instead of using the designer to bind your control to the code (by adding the handles clause to the method), you could use the AddHandler call to manually subscribe to the events you want after you have set the DataSource
eeyoAuthor Commented:
These both work when the form is loading, but what about when there is a position change for BindingSource2?  I am not sure which events to use to turn on/off the flag to prevent the SelectedIndexChanged from firing.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Éric MoreauSenior .Net ConsultantCommented:
You can also call RemoveHandler on the same events before changing the datasource
eeyoAuthor Commented:
If the bindingsource.position changes when a user clicks on a different row in the bound datagridview, which Datagridview or Bindingsource event can I use before any of the bound combobox events fire?  Both the combobox and datagridview are bound to the same bindingsource.
eeyoAuthor Commented:
No perfect resolution.  Perhaps having a bound combobox that is used to make selections on another bound datarow is too complicated.  It's easier to just use a pop-up form to work on a single bound datarow at a time.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.