I want to have a textbox control that suggests and append values from a database in a Windows application with C# 2008 and LINQ.
I do it with a combobox but I can\'t
There are two ways to accomplish this textbox effect:
Either using the graphic user interface (GUI); or with code
Using the Graphic User Interface:
Go to: "Properties" Tab; then set the following properties:
However; the best way is to create this by code. See example below.
AutoCompleteStringCollection sourceName = new AutoCompleteStringCollection();
foreach (string name in listNames)
{
sourceName.Add(name);
}
txtName.AutoCompleteCustomSource = sourceName;
txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
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;
}
}
}
To AutoComplete TextBox Control in C#.net windows application using
wamp mysql database...
here is my code..
AutoComplete();
write this **AutoComplete();** text in form-load event..
private void Autocomplete()
{
try
{
MySqlConnection cn = new MySqlConnection("server=localhost;
database=databasename;user id=root;password=;charset=utf8;");
cn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT distinct Column_Name
FROM table_Name", cn);
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds, "table_Name");
AutoCompleteStringCollection col = new
AutoCompleteStringCollection();
int i = 0;
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
col.Add(ds.Tables[0].Rows[i]["Column_Name"].ToString());
}
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = col;
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
cn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}