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

31,893

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

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

Share:
31,893
SamuraiJack
Author by

SamuraiJack

Updated on July 09, 2022

Comments

  • SamuraiJack
    SamuraiJack almost 2 years

    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?

  • SamuraiJack
    SamuraiJack about 7 years
    If I extract the SelectionChanged logic into a method, I would still need to fire SelectionChanged even in order to call that method won't I?
  • nvoigt
    nvoigt over 5 years
    Can you explain this piece of code? It does not seem to answer the question.
  • Rahul Sahu
    Rahul Sahu over 5 years
    use when need to set current value again to combobox
  • nvoigt
    nvoigt over 5 years
    Both lines won't compile, the first line could be called a typo, but the second just plain does not exist. Please edit your post to explain what this code does and to fix those errors.
  • nvoigt
    nvoigt over 5 years
    Then I guess you did indeed not answer the question, because this will not compile with C#/WinForms or VB.NET/Winforms (nor any other .NET Framework and language combination), which the question is about.
  • Goal Man
    Goal Man almost 3 years
    I'm going to add a comment just in case someone new to WinForms believes an event handler's logic must be fully within the handler and therefore can only be called by raising the event. As @atomaras correctly stated, place the logic you want to call in its own method. Your event handler can call that method and any code that also needs this common logic can directly call the method without using the event handler. This approach is also very helpful to avoid repeating blocks of code in various event handlers.