Reputation: 348
I need to create a text box which will auto resolve what the user types based on a list of values, something similar to the "To" field in e-mail clients.
For example, my list has these values:
Car House Tree
Typing "hou" would automatically resolve to "House", and trying to delete a single letter (with backspace or delete) of that word would delete it entirely, also it would not allow one to mess with an already resolved word (for example "Hou[something]se").
Of course I could create this using a regular text box and controlling what is typed and keeping a strucure with the resolved words..... there are a lot of exceptions to deal with, though, and I'm not willing to reinvent the wheel. Has anyone implemented something like that before? Is there any control in visual studio that provides something similar to what I described?
Update
Note that I understand that the regular text box supports autocompletion, but besides autocompletion I need autoresolution, wich is a different concept...
Upvotes: 1
Views: 397
Reputation: 11519
I would create a usercontrol containing a flowlayout-control. The flowlayout control will autoresize to the usercontrols size.
At start the flowlayoutcontrol will contain one focused textbox whitout any visible borders. The user enter the text and for each keystroke you try to resolve the word. If it resolves as a word, then add a label before the textbox, the label has the resolved word as text and its font is underlined. And the textbox now is cleared for new entry. If the user is pressing backspace or left in the empty textbox, select previous control in flowlayout and make that label font to bold and wait for (delete to delete the word or leftarrow and rightarrow to navigate to another word or the empty textbox). When standing on a resolved word: If pressing left, then move the textbox so it now is to the left of the resolved word. If pressing right, move the textbox to the right of the resolved word.. Doublecklick a label removes the label and activates the textbox with the text ready to edit.
By working with labels (=resolved words) and a textbox for input of new words its pretty easy to emulate the To-field in email-programs. The only thing that is "hard" is to let the user select multiple words and delete, but there are many solutions out there (google +flowlayoutcontrol +multiple +select). And it is also possible to have autocomplete on the textbox to "resolve" to a known list of words.
I have a usercontrol on one of my computers, will look after it today. If I find it I'll upload the source and update the answer with a link to the zip-file.
Edit:
Did'nt find the program so I spent 30 minutes rewriting it from scratch using (flowlayoutcontrol,label,textbox) as a usercontrol
Movie of it live: http://www.youtube.com/watch?v=1MJ7bmFcCmE
Of course, this 30-minutes hack is not perfect. Some navigational stuff is left to implement. MAybe I fix it tonight. If anyone improves on this example code, please post it in this thread for all to benefit.
Here are the sourcecode for this example: http://www.filefactory.com/file/cda1d84/n/testproj.zip
Upvotes: 0
Reputation: 62397
The standard TextBox
control already supports this via the AutoCompleteSource
, AutoCompleteMode
, and AutoCompleteCustomSource
properties.
You can select a standard complete source using AutoCompleteSource
or you can specify a custom list, and then provide an AutoCompleteStringCollection
object to the AutoCompleteCustomSource
property. Although I haven't tried it, I suspect that this is not quite as flexible as the Win32 variation on this where you can dynamically provide auto-complete results based on what has been typed so far, but you may be able to mimic this by overriding AutoCompleteStringCollection
.
This answer was provided before the question changed to indicate the OP already was aware of this information.
Upvotes: 2