Search code examples
c#winformseventslistboxselectedindexchanged

ListBox.SelectedIndexChanged - can you determine if it was user initiated?


My question is similar to this: How to prevent ListBox.SelectedIndexChanged event?, but I want to ask it a different way.

Is there a simple way to determine if the 'SelectedIndexChanged' is coming from the user as opposed to initiated through code (e.g. ListBox.SelectedIndex = x)?


Solution

  • As far as I know, no, there's no simple way built-in. The best I've been able to do is set a flag just before changing it in code and then letting the event handler reset the flag and return.

    I suppose you could start examining the call stack and see if it's originating somewhere in your own code or not, but I'm not sure how much it's worth the effort.