views:

559

answers:

6

I would like to have a TextBox that supports AutoComplete and lets users type multiple words separated by a comma or semicolon, offering suggestions for each word. I have a standard TextBox with

textBox.AutoCompleteCustomSource.AddRange(new[] { "apple", "banana", "carrot" });
textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;

Unfortunately it will only suggest for the first word. Anything typed after that and it stops suggesting.

I want to be able to perform the following scenario:

  1. Type "ap"
  2. Have it suggest "apple"
  3. Press the comma
  4. Have it fill in "apple," with the cursor after the comma
  5. Type "ba"
  6. Have it suggest "banana"
  7. Press the comma
  8. Have it append "banana," resulting in "apple,banana,"

I've tried Googling for a solution, but haven't had much luck. This seems to be a popular feature for web apps, but apparently not for winforms. Any suggestions?

A: 

You can implement your own autocomplete by listening to the KeyDown event.

leson
+2  A: 

I don't believe you can complete this task with just the build in autocomplete textbox properties. What I would do is make some custom functionality which checks the OnTextChanged method for the text box and decides what to do. Granted this will be a bit more involved compared to what you were trying to do. You'll have to decide if they are typing some known string and give the suggestions in some sort of selectable manner, append the selected change if they click it, catch when they type a delimiter and append the currently selected suggestion, and ready yourself for them to add more text and start the process again.

I hope someone else has an easier way but if not, hopefully this gives you an idea of the logic needed. Good luck!

Tim C
A: 

Please let me know if you solved this... i need to do the same thing...

shawn
+1  A: 

Check this textBox extension http://pholpar.wordpress.com/2010/02/25/multivalue-autocomplete-winforms-textbox-for-tagging/

Sachin Ranadive
Nice! This looks very promising. I've had to put off playing with this for now, but I'll definitely give it a try when I have a chance. The code seems to make sense. Thanks!
Ecyrb
A: 

Take a look at http://php.scripts.psu.edu/rja171/widgets/autocomplete.php

wcheng
That's not for winforms.
Ecyrb
A: 

Use JQuery autocomplete for this purpose.

klusner
We're talking about winforms, not web apps.
Ecyrb