autocomplete

  • Thread starter Thread starter Doug
  • Start date Start date
I have a simple form, just with one combo box and an OK button - and i
have tried to use the autocomplete routine and I have enabled
autocomplete in
the combo box properties.

http://www.java2s.com/Code/CSharp/Components/UseanAutocompleteComboBox.htm

That is the link that I copied the code from - but the box doesnt
autocomplete and I dont know what i am doing wrong.

I don't really understand your question. The code you refer to in the
link has nothing to do with using the built-in auto-complete attributes of
the .NET ComboBox control.

The code you refer to in the link has a very basic auto-complete. The
AutoCompleteComboBox defined in the example only matches from things
actually in the list of the control. It should do this just fine, without
you having to set any additional properties. Just use that class if the
behavior is what you want.

On the other hand, the built-in auto-complete functionality of the .NET
ComboBox class is more powerful, and also more complicated. Note that at
a minimum, you need to set the AutoCompleteMode property to something
other than None, *and* the AutoCompleteSource to something other than
None. If you want to provide a custom list of things to match against,
you need to use the AutoCompleteCustomSource, as well as setting
AutoCompleteSource to CustomSource.
Is there another step that I am missing???

See above. My guess is that you copied the example code without
understanding what it's doing, and either haven't used the
AutoCompleteComboBox it defines, or you haven't populated the list for the
control so that it has something to match against.

But since you say you also have "enabled autocomplete in the combo box
properties", it sounds like you are *also* trying to use the built-in
functionality at the same time. I think you should probably pick one or
the other and stick with that. If you want to use the built-in
functionality, then just toss out the code example you've referred to and
make sure you've set *all* of the appropriate properties to enable the
built-in functionality to work the way you want.

Pete
 
hi Peter

I am still struggling with this. Could you please post some instructions
about how to achieve autocomplete for a combo box?

I have been trying this for about a week and have no joy in my work.

Doug
 
I am still struggling with this. Could you please post some
instructions about how to achieve autocomplete for a combo box?

Well, depending on what you want to do, you may not need any code (that
you wrote).

The most basic example would be to add the ComboBox to your form in the
designer. Then, in the properties for the ComboBox, find the
AutoCompleteMode, AutoCompleteSource, and AutoCompleteCustomSource
properties. AutoCompleteMode gets set to one of three values: Append,
Suggest, or SuggestAppend. AutoCompleteSource can be set to
CustomSource. And then in AutoCompleteCustomSource, when you put your
mouse over the property value you'll see a little "..." button. Click
that and you can add whatever strings you want the auto-complete
functionality to operate against.

If you want functionality beyond what's provided that way, you'll need to
write code to configure those properties programmatically. But the idea
is the same.

If all you want is an auto-complete that matches against the items already
in the ComboBox's list, then you may find that it is easier to just use
the sample code you referred to. In that case, forget you ever saw the
"AutoComplete..." properties on the built-in ComboBox and just use the
derived control in the sample code you found.

Pete
 
Hi Peter

What I am after is some additional functionality for a combo box so that
items that the user has already typed in - in previous sessions of use -
will be suggested in the combo box - but they can choose to ignore those
suggestions - and any new suggestion will be auto-added to those suggestions
for their next use session - similar to the search boxes on google etc.

Thanks for your help so far.

Doug
 
What I am after is some additional functionality for a combo box so that
items that the user has already typed in - in previous sessions of use -
will be suggested in the combo box - but they can choose to ignore those
suggestions - and any new suggestion will be auto-added to those
suggestions
for their next use session - similar to the search boxes on google etc.

I recommend, then, that you maintain a list of those entries in a
StringCollection. Save them to the user's settings (use the project's
Properties.Settings.Default property to store them), and use that
collection to initialize the combo box's AutoCompleteCustomSource property
any time you use the combo box.

Hook an event handler to the Validate event of the combo box, and every
time the combo box gets validated, add the newest entry to that
StringCollection. If you want to limit the size of the StringCollection
(not a bad idea :) ), you can do something like removing the string from
the StringCollection and reinserting it either at the end or the beginning
of the collection, when you are adding a string to the collection (you'll
want to look for the string in any case, so to avoid wasteful duplicates),
and then if the collection gets larger than some maximum size you pick
(50, 500, whatever) remove an item from the opposite end (that is, if new
items get added to the beginning, remove an item from the end to make room
once you've reached your maximum size).

Note that for some common types of information (URLs, filenames, etc.) the
combo box already supports them automatically. The above is something
you'd use if you are saving types of information that don't fall into the
existing categories supported by AutoCompleteSource (see the docs for the
full list).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top