Autocomplete mode for textbox in C# .net

AutoComplete Mode for TextBox control in C#

               Textbox control have the AutocComplete mode in C Sharp language.When one perticular field will be with related data that time we can set the autocomplete mode to the textbox.This feature does not there in the VB.Net language.When using the vb.net code user must enter the entair field.Autocomplete mode is very usefull feature to the user. Autocomplete mode will work like Google search engine. While user entering the data, the word completion will come as a dropdown to the textbox from a perticular field from database.For that you have to write the select statement from that perticular database.




Properties those you have to set to the textbox :

AutoCompleteMode  : Suggest Append
AuoCompleteSource : CustomSource


These properties must set to the textbox that to which textbox you want to set the AutoCompleteMode.
Then only the code will be performs.


Event: This code should write in Page_Load event.


            SqlCommand cmd = new SqlCommand("select name from databasetable", con);
            SqlDataReader dr = cmd.ExecuteReader();
            AutoCompleteStringCollection col = new AutoCompleteStringCollection();
            while (dr.Read())
            {
                col.Add(dr.GetString(0));
            }

            textBox1.AutoCompleteCustomSource = col;

I hope this code will help you.

2 comments: