I am trying to create a TextBox with auto completion.
In the constructor of my Form, I am getting data from a database and set the TextBox AutoCompleteCustomSource
property to the user names array.
For some reason, the autocomplete feature is not working.
I am sure that there are no problems with the db.getUsersList()
method (screenshot at the bottom).
public mainPanel()
{
InitializeComponent();
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(db.getUserList().ToArray());
nickName.AutoCompleteCustomSource = collection;
}
To setup a control that supports auto completion, it's necessary to specify the source of the AutoComplete feature. When set to a string collection using the AutoCompleteCustomSource property, the AutoCompleteSource property must be set to AutoCompleteSource.CustomSource and AutoCompleteMode set to either
AutoCompleteMode.SuggestAppend
orAutoCompleteMode.Suggest
.These properties must be used together to specify how the AutoComplete feature works.
Since it appears the code in the question is using some sort of data source to create the
AutoCompleteCustomSource
collection, here's a generic example that creates aCustomSource
from aList<class>
, adds bindings to the controls using aBinding
class and updates the values of some controls using aBindingSource
.The example, as seen it visual sample, uses three control: a TextBox (
txtAutoComp
), where the AutoComplete feature is enabled, and two Labels (lblNickName
andlblNickValue
), bound to the same data source, which are updated when the text of AutoComple control changes.The AutoComplete is expanded to allow to find elements using partial strings, either clicking a Button (
btnFindNick
, here) or pressing theEnter
key in the TextBox: