Thursday, 23 October 2008

Autocomplete textboxes

So we had this interesting issue on this WinForms project I have been working on. We have been making quite extensive use of autocomplete textboxes in our application and one of our testers raised a continuity issue that when they selected an auto-complete option from the list with their mouse it behaved differently from when a user hit the tab button. After some quick investigation it became apparent that there were no events to subscribe to on the auto-complete part of the control to modify the behaviour. I ran this issue past a number of my peers and we were quite stumped.

However thanks to some great work by a colleague who took it on as a personal challenge (thanks Faranak and your uber googling skills ) we managed to resolve the issue we were having. It appears that although Microsoft have effectively buried all the events and monitoring of the auto-complete part of the text box way down in the windows process there is a relatively simple fix.
When you click on one of the auto-complete text options or when you scroll down and press enter then the same thing happens. A KeyDown event is thrown with the ‘Enter’ key marked as being pressed regardless of whether it was a mouse click or the enter button pressed. Thus all we needed to do was monitor for the KeyDown event and the enter key, check that the text matches the list of codes (the auto-complete text source) for the textbox and implement our required outcome. (Cue much rejoicing)

No comments:

Post a Comment