Auto-complete is a feature provided by C1TextBox control which involves the textbox to predict a word or phrase that the user wants to type in without the user actually typing it in completely. This feature can simply be set using AutoCompleteMode and AutoCompleteSource properties. We can set the AutoCompleteSource to a datatable. However, there are requirements wherein you would want to create a textbox with “dynamic lookup." This means instead of a fixed list of items to search, a list of suggestions will be shown as per the text entered in the textbox. In this blog we'll discuss how to show dynamic autocomplete source based on the entered value. The basic logic for dynamic autocomplete is set the C1TextBox's AutoCompleteCustomSource based on the text entered. For simplicity, the logic that we'll be showcasing is to show ProductName column of the Product's table if alphabets are entered and ProductId column if the numbers are entered. You may modify the logic as per your requirement.
To enable the Auto-complete feature in C1Textbox, we'll set the AutoCompleteMode to Suggest and AutoCompleteSource to CustomSource. Also, we'll create a AutoCompleteStringCollection object which will be changed as per the text entered in the textbox.
AutoCompleteStringCollection list;
string[] arr;
private void Form1_Load(object sender, EventArgs e)
{
list = c1TextBox1.AutoCompleteCustomSource;
c1TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
c1TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
}
If you want to show database values in C1TextBox, you simply need to add the database column values in the string array and add this collection to AutoCompleteStringCollection object. However, for creating dynamic lookup, we change the autocompletesource in the TextChanged event.
For the dynamic list generation, we'll be using a custom GetList() method. This method returns the string array object arr to ProductId or ProductName based on the C1TextBox's Text. The complete code for this method is:
String[] GetList(string s)
{
string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\\ComponentOne Samples\\\Common\\\C1Nwind.mdb;";
OleDbConnection conn = new OleDbConnection(connstr);
string cmd = "SELECT ProductID, ProductName FROM Products";
OleDbDataAdapter da = new OleDbDataAdapter(cmd, conn);
DataSet ds = new DataSet();
da.Fill(ds);
int count = ds.Tables[0].Rows.Count;
arr = new string[count ];
try
{
int check = int.Parse(s);
for (int i = 0; i < count; i++)
{
arr[ i ] = ds.Tables[0].Rows[ i ]["ProductID"].ToString();
}
}
catch
{
for (int i = 0; i < count; i++)
{
arr[ i ] = ds.Tables[0].Rows[ i ]["ProductName"].ToString();
}
}
return arr;
}
The method will be used as following in the TextChanged event as:
//Dynamic AutoCompleteCustomSource
private void c1TextBox2_TextChanged(object sender, EventArgs e)
{
if( GetList(c1TextBox1.Text).Length >0)
{
list.Clear();
list.AddRange(arr);
}
c1TextBox1.AutoCompleteCustomSource = list;
}
Along with this, we will also handle the PreviewKeyDown event to handle the Up and Down keys.
private void c1TextBox2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if((e.KeyCode == Keys.Up )||(e.KeyCode == Keys.Down))
check = false;
else
check =true;
}
Download the sample for complete implementation of the above code. Download Sample