hi
I want to have a textbox control that it give suggest and append from a database in win application whit C# 2008 and LINQ .
I do it whit Combobox but I can't do it whit textbox .
How do I do it?
hi
I want to have a textbox control that it give suggest and append from a database in win application whit C# 2008 and LINQ .
I do it whit Combobox but I can't do it whit textbox .
How do I do it?
You could attach to the KeyDown event and then query the database for that portion of the text that the user has already entered. For example, if the user enters "T", search for things that start with "T". Then, when they enter the next letter, for example "e", search for things in the table that start with "Te".
The available items could be displayed in a "floating" ListBox, for example. You would need to place the ListBox just beneath the TextBox so that they can see the entries available, then remove the ListBox when they're done typing.
Check out the AutoCompleteSource
, AutoCompleteCustomSource
and AutoCompleteMode
properties.
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection col = new AutoCompleteStringCollection();
col.Add("Foo");
col.Add("Bar");
textBox1.AutoCompleteCustomSource = col;
Note that the designer allows you to do that without writing any code...
of course it depends on how you implement it but perhaps this is a good start:
using System.Windows.Forms;
public class AutoCompleteTextBox : TextBox {
private string[] database;//put here the strings of the candidates of autocomplete
private bool changingText = false;
protected override void OnTextChanged (EventArgs e) {
if(!changingText && database != null) {
//searching the first candidate
string typed = this.Text.Substring(0,this.SelectionStart);
string candidate = null;
for(int i = 0; i < database.Length; i++)
if(database[i].Substring(0,this.SelectionStart) == typed) {
candidate = database[i].Substring(this.SelectionStart,database[i].Length);
break;
}
if(candidate != null) {
changingText = true;
this.Text = typed+candidate;
this.SelectionStart = typed.Length;
this.SelectionLength = candidate.Length;
}
}
else if(changingText)
changingText = false;
base.OnTextChanged(e);
}
}
I'm not sure this is working very well, but I think the base of this code is good enough.
This might not be the best way to do things, but should work:
this.textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.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 >= 3)
{
//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;
}
}
}