tags:

views:

84

answers:

3

If you supply a list of strings to an edit control and set the autocomplete mode and source then you automatically get autocomplete functionality. My question is can I get the same functionality in .NET somewhere without a control. In other words I want something like:

string[] ProgressivePartialMatch( string[] Strings, string MatchText )

and so I want the strings back that would have showed up in the autocomplete, so to speak.

A: 

You can use ajax to get matched items from database (jQuery will fit your needs). And simple javascript (preferably jQuery) for edit control. The question is why you need this?

P.S. Take a look at this

http://stackoverflow.com/questions/305994/jquery-autocomplete-and-asp-net

Sorantis
I don't think this is what he wants. He rather wants a function that returns the items which match the pattern.
Lukasz Lysik
@Lukasz correct I am looking to basically run StartsWith on an array and while I can certainly iterate over the array I was wondering if the framework exposes the underlying autocomplete algorithm ,that the edit control uses, in some way
Rahul
A: 

If it doesn't exist, it's easy to write yourself

string[] ProgressivePartialMatch(string[] Strings, string MatchText)
{
    return Strings.Where(s => s.StartsWith(MatchText)).ToArray();
}
Gabe
This will work, but it will be awfully slow for large data sets since it has to go through *all* the items.
Zach Johnson
It wouldn't be too bad, sort the string array and than do binary search on it. It wouldn't have to go through all of the items than.
Casey
Yes, it could be slow on a large list, but it would get smaller and smaller for every character typed. If the input is a sorted array I could do a binary search, but that wasn't part of the specs.
Gabe
this is basically what I am doing now BTW :-) My data size wont be more than 5 to 8 K strings so should be okdoes anyone know what the actual Edit control does to get this functioanlity going
Rahul
The Winforms autocomplete functionality is actually implemented by a built-in COM object. Needless to say, this is the best solution.
Gabe
same problem with the COM object, SHAutoComplete basically hooks up an edit control to the implementation , I guess this feature is not exposed outside of the context of an edit control or combo box etc
Rahul
seems kind of insane but I guess one could subclass an edit control hide it and then run the partial string through it to get back the list of string in the suggest windowhmm anyway why bother :-)
Rahul
+1  A: 

If you want fast autocomplete, you're going to want to implement a trie. You can find all the items that start with a particular string by following the trie down until the "starts with" string ends.

Zach Johnson
What if he wants to find all strings that contain the MatchText instead of just prefixed with it?
Gabe
@gabe: True, this solution won't work if that is the case. I guess the answer to the question depends on what the asker defines as autocomplete.
Zach Johnson
I think just starts with is fine thats how the edit control autocomplete seems to work as opposed to contains
Rahul