I am writing on c# with visual interface. Part of the logic consists of a set of values changing depended on which one of them (sets) is selected in the combo box.
Changes in the value sets can be saved or not. There is a need to offer an opportunity for user to save unsaved changes or reject them when he chooses a different item (set) in the combo box. It is imperative that when the message box with yes/no is presented combo box still displayed the old value, and only after that, depending on user’s choice displayed new or old.
The sequence should be:
user uses keys or drop-down to select new item -> event is fired and form stops all of its processing -> my code cancels the change or lets it go through -> (if not cancelled) combo box is redrawn with new value.
N.B. Following events were tried and proved not to be adequate:
- SelectedIndexChanged
- SelectedValueChanged
- SelectionChangeCommitted
- Validating
- DropDownClosed
I have found somewhat acceptable solution, although not directly corresponding to the verbatium of the question asked.
One possible solution, though, sadly, for some reason deleted by his suggestor, was to use message filters. That, however, led to the path of manually calculating where the mouse click went and essentially substituting the winforms capabilities of translating the mouse events to the process of changing selected item in dropped list of the combo box with some crude crutches on my own. This is the path i shied from.
In the end i settled on “cosmetic” solution, with the idea being substitution of displayed text in combo box for the duration of user’s decision-making on the subject of whether or not cancel the change.
So, in the SelectedIndexChanged event i’ve put the follofing code:
The gist of the idea is this: in DropDown style ComboBox’ text can be changed as needed. However, when set to one of the items in the list, a change of selection will occur. To avoid that unneededly, a space is added to the temporary text.
In case cancelling does not occurs, restoring of the style to DropDownList forces the text to be changed to the actual chosen value (which has remained the same).
In case user cancels the change, value of the combo box is set back to the old one. A check in the beginning of the handler stops the event generated by that from being processed further.