Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Assume that I have a dropdown with 2 items and by default, the first item is selected. If I select the click the first item in the dropdown, is there a way I can get the selectedIndexChanged event to still fire?
Posted

No the event will not fire because the index has not changed
 
Share this answer
 
No, because you really don't change the selection, but if the event was added to the invocation list before it was selected programmatically, you will have this event fired without a click, which is not what you want.

You can do the following. Add a separate method to implement all the code you put in the event handler, because you will need to call if from more than on place. Call this method from your event. This way you have re-factored the code, that is, no functionality was changed so far. Add another event handler to the invocation list of the event Click, and call the separate method mentioned above on condition, only when it happens for the first time (add a flag "ClickedFirstTime" or something like that). Even though it will give you the effect you wanted, I don't really recommend doing it. Rather, I would advice you to fix the design of the code. See below.

Yes, this question tells me that you rather have a problem in your code design, not in implementation of the particular technique. The paragraph above should give you an idea. If selection is not changed, you should not fire or simulate the event intended to be used on selection change. The call to the separate method I mentioned below should be done before all those control events and be a part of the UI population.

—SA
 
Share this answer
 
Comments
Addy Tas 6-Dec-11 18:13pm    
5+ first think design, then think of fixing code
Sergey Alexandrovich Kryukov 6-Dec-11 18:54pm    
True. Thank you Addy.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900