This is a problem I've had to deal with in my last project, and although I found a working solution, I wasn't too happy with it and am wondering if there would've been a better, cleaner one.
Problem:
Basically, I needed to implement a ComboBox that inserts or appends an item (selected from the drop-down list) to the textbox instead of replacing all text in the textbox:
- If the textbox has some text selected, that selection would be replaced with an item selected from the drop-down list;
- If the textbox has no text selected, an item chosen from the drop-down list would be appended in the textbox field.
As it turned out, I could not achieve this behaviour by handling a combination of ComboBox
's events (such as SelectedIndexChanged
, SelectionChangeCommitted
, TextChanged
etc.) because the ComboBox
control would finally synchronize the textbox field with the selected item from the drop-down list without raising any further events afterwards.
Solution, a.k.a ugly hack:
I ended up installing a Timer
that, once expired, causes an update to the textbox field. The timer was set to approx. 30 ms, which should be long enough to ensure that all events (SelectedIndexChanged
, TextUpdate
etc.) have been processed, and short enough to not feel like a noticeable lag to the user.
Does anyone know of a cleaner solution to this problem?