问题
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