Events firing when I don't want them to

  • Thread starter Thread starter David Jackson
  • Start date Start date
D

David Jackson

Hey,

I'm new to C#, so sorry if this is a dumb question.

I have a WinForms app with a form. The form has two combo boxes which which
have SelectedIndexChanged methods.

The user selects a record in the first combo box and its
SelectedIndexChanged method fires which changes the selected item in the
second combo box. Problem is that this causes the second combo box's
SelectedIndexChanged method to fire, which I don't want. I only want the
second box's SelectedIndexChanged method method to fire if the user changes
the selection, not if my code changes the selection.

How do I do this?

DJ
 
As a general rule, most events just fire to tell you (correctly) that
something changed; they don't explain how... a common workaround here
would be to set a bool before making the change (or increment a
counter if re-entrancy is an issue), and only do something in the
second handler if the marker is false / zero.

The fact is; the selected index *has* changed, so it is reasonable for
SelectedIndexChanged to fire.

Marc
 
As a general rule, most events just fire to tell you (correctly) that
something changed; they don't explain how...
OK.

a common workaround here would be to set a bool before making the change
(or increment a counter if re-entrancy is an issue), and only do something
in the second handler if the marker is false / zero.

I'll do that.
The fact is; the selected index *has* changed, so it is reasonable for
SelectedIndexChanged to fire.

I suppose so. When I was Googling for this I found this page
http://www.codeproject.com/purgatory/acomboboxfix.asp which looks like the
exact opposite of my problem which is why I thought I had made a mistake.

Is there no way in C# to say that an event will only fire in response to
user input, apart from to use your suggestion of a bool?
 
David,

No, generally there is not. There would have to be a event created
specifically for that.
 

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

Back
Top