1

Possible Duplicate:
C# AutoComplete

I've a standard winform combobox. I've set its AutoComplete property to true. I want to change the comparison between typed text and items text which is done automatically by the UI.

Something like:

autoCompleteCombo.XXXX = new Func<string, string, bool> { (search, item) => item.Contains(search) };

Note: the function wrote is just an example. What I really want a little more complex.

Community
  • 1
  • 1
Diego
  • 16,436
  • 26
  • 84
  • 136

1 Answers1

0

Since you've updated your question, I understand your question better. You've also said the underlying data and function aren't pertinent, which makes it difficult to understand exactly what you're trying to achieve, so my recommendation would be to create a custom ComboBox and see if you can handle the matching on your own.


I think the most elegant way to write a function to test whether the typed text is an item in a ComboBox would be to use an extension method. Your calls would look like this:
// see if the Text typed in the combobox is in the autocomplete list
bool bFoundAuto = autoCompleteCombo.TextIsAutocompleteItem();

// see if the Text type in the combobox is an item in the items list
bool bFoundItem = autoCompleteCombo.TextIsItem();

The extension methods could be created as follows where you can customize exactly how your search logic is to work. In the two extension methods I wrote below, they simply check to see if the text typed into the ComboBox is found in the AutoCompleteCustomSource collection, or, in the second function, if the text is found in the Items collection.

public static class MyExtensions
{
    // returns true if the Text property value is found in the 
    // AutoCompleteCustomSource collection
    public static bool TextIsAutocompleteItem(this ComboBox cb)
    {
        return cb.AutoCompleteCustomSource.OfType<string>()
            .Where(a => a.ToLower() == cb.Text.ToLower()).Any();
    }

    // returns true of the Text property value is found in the Items col
    public static bool TextIsItem(this ComboBox cb)
    {
        return cb.Items.OfType<string>()
            .Where(a => a.ToLower() == cb.Text.ToLower()).Any();
    }
}   
Brad Rem
  • 6,036
  • 2
  • 25
  • 50
  • And where would I use it?? Could you guide me a little more? The second code are the methods. Ok with that, the first code where should I write it? – Diego Apr 24 '12 at 11:24
  • You can place the `MyExtensions` class in its own file in your project, @Diego. Just keep it in the same namespace as your forms. Then, at the place in your code where you need to check to see if the typed text is an item in the combobox you call `autoCompleteCombo.TextIsItem();`. – Brad Rem Apr 24 '12 at 14:39
  • I think we have a missunderstanding. What I want to do is to have all the functionality of AutoComplete but when it (code from ComboBox) searchs which items match, it uses a custom comparer. – Diego Apr 24 '12 at 14:47
  • @Diego, you're going to have to edit your original question and include the class you are binding your combobox items and/or autocomplete to as well as a description for how you expect the typed text should be matched to the underlying items. – Brad Rem Apr 24 '12 at 17:15
  • I've edited my question to avoid misunderstandings. I thought it was clear enough. I don't think the type of comboxbox items or the function to define match are pertinent here. – Diego Apr 24 '12 at 17:29
  • @Diego, I think your only hope is a custom ComboBox. You may have to go as far as create your own combbox that draws the autocomplete items in the list on the screen. – Brad Rem Apr 24 '12 at 18:44