Using the AutoComplete feature of ComboBox, while limiting values to those in the list?

Posted by Schmuli on Stack Overflow See other posts from Stack Overflow or by Schmuli
Published on 2009-01-14T16:12:37Z Indexed on 2010/06/08 8:02 UTC
Read the original article Hit count: 191

Filed under:
|
|

In WinForms 2.0, a ComboBox has an Auto-Complete feature, that displays a custom Drop-Down list with only the values that start with the entered text.

However, if I want to limit valid values to only those that appear in the ComboBox's list of items, I can do that by setting the DropDownStyle to DropDownList, which stops the user from entering a value.

However, now I can't use the Auto-Complete feature, which requires user input.

Is there another way to limit input to the list, while still allowing use of the Auto-Complete feature? Note that I have seen some custom solutions for this, but I really like the way the matching Auto-Complete items are displayed in a Drop-Down list, and sorted even though the original list may not be.

EDIT: I have thought about just validating the entered value, i.e. testing user input if it is valid in, say, the TextChanged event, or even using the Validating event. The question then is what is the expected behavior? Do I clear their value (an empty value is also invalid), or do I use a default value? Closest matching value?

P.s. Is there any other tags that I could add to this question?

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms