SamuraiJack
SamuraiJack

Reputation: 5569

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

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?

Upvotes: 10

Views: 33165

Answers (3)

Rahul Sahu
Rahul Sahu

Reputation: 1

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

Upvotes: -2

Sinatr
Sinatr

Reputation: 22008

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

Upvotes: 18

atomaras
atomaras

Reputation: 2568

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?

Upvotes: 28

Related Questions