0

The question is similar to What event handler to use for ComboBox Item Selected (Selected Item not necessarily changed) , albeit with WinForms.

When the user selects an item in a ComboBox, even if the item was selected before, I want to execute some additional code. If the user just opens and closes the dropdown, I don't want the code to run.

SelectionChanged, ValueChanged and DropDownClosed so far did not work. ValueChanged is best, but not 100%ly what I want. I would like a ValueSelected event.

Andreas Reiff
  • 7,961
  • 10
  • 50
  • 104

1 Answers1

1

WinForms' ComboBox raises the SelectedIndexChanged and SelectionChangeCommitted events anytime an item is selected, not matter if it is the same index.
These events are not raised if the DropDown is simply opened and closed.

Jimi
  • 29,621
  • 8
  • 43
  • 61
Boris B.
  • 4,933
  • 1
  • 28
  • 59
  • Shame, but I thought the same after searching for a while. Clearing the selection is not an option to prevent user confusion. Inheriting is an option but seems overkill. And even then I would need to see that I cover all cases - and generally have bad experience with own controls that should work in 100% of scenarios. – Andreas Reiff Jan 22 '20 at 12:02
  • @AndreasReiff I know what you mean, comboboxes are one of the most tricky controls to implement properly, on any platform. Our JS implementation of a combobox was 10x the size of our grid component for example, there's just so many things to cover. Just out of curiosity, what's the actual use case for your problem? Is is just a client's request or something else? – Boris B. Jan 22 '20 at 12:08
  • The comboboxes hold parameters for a sql query, e. g. filter on company x, filter on region y. Those filters are optional. That part is reflected by a checkbox for each combobox that enables/disables the parameter in the query. I want to set the check automatically if the user might expect he already enabled the parameter by "fiddling" with it. – Andreas Reiff Jan 22 '20 at 12:56
  • @AndreasReiff Why don't you simply disable (and clear) the combobox if its corresponding checkbox is unchecked? That way in order to "fiddle" with the combobox they have to conscientiously enable that field filter. Or get rid of checkboxes and have item zero be the [No filtering] item. – Boris B. Jan 22 '20 at 12:58
  • To provide meaningful defaults. Given the example here, it is difficult to give something concrete, but we use DateTimPickers too, and there we have defaults like beginning of week/month/quarter as preset, even if the parameter is not enabled, since that is the first choice if someone actually wants to filter on date. BTW, we always had the conscientious effort needed on the checkbox, but now we got the first complains about the extra click and forgetting it (and then the query perhaps running for a minute with wrong parameters9. – Andreas Reiff Jan 22 '20 at 13:09
  • @RezaAghaei I cannot delete the accepted answer, so unless someone posts an answer and the op accepts it, I can't do much. – Boris B. Jan 22 '20 at 14:52
  • No worries, • You can wait until the user unaccepts the post and then you can delete it. • You probably can flag your own post for an administrator to delete it (I'm not sure about it). • Also as another option you can ask Jimi if he is going to post an answer, let him post the answer, otherwise if Jimi is not going to post an answer, since regarding to the comments under the question it seems the `SelectedIndexChanged` event is an acceptable answer for the user, you can edit your post, refer to Jimi's post and add more details. Too many options? :D – Reza Aghaei Jan 22 '20 at 15:02
  • Thank you. "Not possible" is an answer I frequently get. So I accepted. Anywho, better this way. – Andreas Reiff Jan 22 '20 at 16:29