I have a textbox used to type in strings and display all the avaiable results in this textbox.The current code like follows:
private void Form_Load(object sender, EventArgs e)
{
TextBox.AutoCompleteMode = AutoCompleteMode.Suggest;
TextBox.AutoCompleteSource = AutoCompeteSource.CustomSource;
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
if(t != null)
{
if(t.Text.Length > = 1)
{
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(s.Name);
this.TextBox.AutoCompleteCustomSource = collection;
}
}
}
In the above code, s.Name
is the source of all the strings I want to search. It shall work only I correctly typed the first letter of the string. For example. One of the s.Name
may be ABCDEF
I want it avaiable when I type any sub string of it, maybe EF
or BC
but not only AB
or ABC
. How should I do this? Thanks!
I am not going to help you like giving all code, so that you can copy or paste. So I am going to give an idea.. Check out the AutoCompleteSource, AutoCompleteCustomSource and AutoCompleteMode properties.
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox t = sender as TextBox;
if (t != null)
{
//say you want to do a search when user types 3 or more chars
if (t.Text.Length >= 1)
{
//SuggestStrings will have the logic to return array of strings either from cache/db
string[] arr = SuggestStrings(t.Text);
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(arr);
this.textBox1.AutoCompleteCustomSource = collection;
}
}
}
Hope it helped. Notify me if you have not understood yet. For more information, you can like this article... http://www.c-sharpcorner.com/article/autocomplete-textbox-in-C-Sharp/