Is it possible to fire ComboBox SelectedIndex Changed Event even when old and new index are same?

寵の児 提交于 2019-12-21 09:39:34

问题


I have a scenario is which I need to fire the SelectedIndexChanged event of a winform's combox even when the old and new index is same.. I can not use SelectionChangeCommited because the values are being set programmatically .. and it wont get fired. Is it by any chance to force 'SelectedIndexChanged' to fire even when old and same index are same?


回答1:


Nothing prevents you from calling event handler directly:

comboBox1_SelectedIndexChanged(comboBox1, new EventArgs()); // or (null, null)

But solution of atomaras is a better (nicer) way to do it.

I myself dislike to use standard components in more-less serious software. Instead I subclass all standard components from very beginning and adding functionality to them as soon as I need it without needs to change anything in the existing forms.

In this case I'd add a public event riser OnSelectedIndexChanged to execute event (to run code inside event handler programmatically).




回答2:


It seems wierd that you want the event to refire for the same item. It's probably because you just want to reexecute the event handler logic. Why dont you extract the SelectionChanged logic into a new method and call that one programmatically?




回答3:


combobox.selectedIndex = value;
combobox.selectedevent(null,null);


来源:https://stackoverflow.com/questions/19829381/is-it-possible-to-fire-combobox-selectedindex-changed-event-even-when-old-and-ne

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