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.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
Rahul
  • 2,194
  • 4
  • 31
  • 46

3 Answers3

1

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
  • 23,678
  • 6
  • 69
  • 86
  • What if he wants to find all strings that contain the MatchText instead of just prefixed with it? – Gabe Mar 28 '10 at 00:38
  • @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 Mar 28 '10 at 00:46
  • I think just starts with is fine thats how the edit control autocomplete seems to work as opposed to contains – Rahul Mar 28 '10 at 00:52
0

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

jQuery Autocomplete and ASP.NET

Community
  • 1
  • 1
Sorantis
  • 14,496
  • 5
  • 31
  • 37
  • I don't think this is what he wants. He rather wants a function that returns the items which match the pattern. – Lukasz Lysik Mar 28 '10 at 00:14
  • @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 Mar 28 '10 at 00:54
0

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
  • 84,912
  • 12
  • 139
  • 238
  • This will work, but it will be awfully slow for large data sets since it has to go through *all* the items. – Zach Johnson Mar 28 '10 at 00:24
  • 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 Mar 28 '10 at 00:28
  • 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 Mar 28 '10 at 00:35
  • this is basically what I am doing now BTW :-) My data size wont be more than 5 to 8 K strings so should be ok does anyone know what the actual Edit control does to get this functioanlity going – Rahul Mar 28 '10 at 00:50
  • The Winforms autocomplete functionality is actually implemented by a built-in COM object. Needless to say, this is the best solution. – Gabe Mar 28 '10 at 01:55
  • 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 Mar 28 '10 at 02:43
  • 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 window hmm anyway why bother :-) – Rahul Mar 28 '10 at 02:45