Auto Complete for Textbox element in C# Desktop App


Step 1: Place the textbox element in your working Window.

Step 2: Change the properties of textbox element as below

Properties

Step 3: Now add Autocomplete list to textbox element. For this step we have two ways.

  1. Add autocomplete  list by adding values to AutoCompleteCustomSource

click on collection button

two

Now add list in “String Collection Editor” window

three

2. Adding list from Database table.

Check the following code

//connecting to database
MySqlConnection con = new MySqlConnection();
con.ConnectionString = “<your connection string>”;
con.Open();

//get values from database
string query= “select <column name> from <table name>”;
MySqlCommand cmd= new MySqlCommand(query, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet Ds = new DataSet();
da.Fill(Ds);
da.Dispose();
Cmd.Dispose();

//create object for AutoCompleteStringCollection
AutoCompleteStringCollection list= new AutoCompleteStringCollection();

//fetch the values and add to list object
foreach (DataRow row in Ds.Tables[0].Rows)
{
list.Add(row[0].ToString());
}

//adding list to your text element
textbox.AutoCompleteCustomSource = list;

 

Example Result:
three

Now build your Application and check the Result.

Thanks for reading
Shreenu