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

匆匆过客 提交于 2019-12-12 14:45:57

问题


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)?


回答1:


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.




回答2:


For me, the 'SelectionChangeCommitted' event was better suited for my purposes. It fires when a selection in the drop down is selected. This is the easiest way to handle the specific case when the end-user initiates the change. SelectedIndexChanged is to capture all cases.




回答3:


Property change listeners don't distinguish between causes of a property change. It's a common problem. Setting a flag is the way to do it.

I do wish that there was a way to set values without firing property change events. But then, people argue that it breaks the whole object-oriented model, because it effectively allows you to change a field directly, without using a property.



来源:https://stackoverflow.com/questions/3071811/listbox-selectedindexchanged-can-you-determine-if-it-was-user-initiated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!