F How to Autocomplete TextBox in C# | CodeTheta

How to Autocomplete TextBox in C#

August 17, 2019

Code:
void AutoCompleteText() {
            string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;
            SqlConnection conn = new SqlConnection(myconnstrng);
            string sql = "SELECT product_code FROM ProductMaster";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                AutoCompleteStringCollection Collection = new AutoCompleteStringCollection();
                while (reader.Read()) {
                    Collection.Add(reader["product_code"].ToString());
                }
                txtInvoiceProductCode.AutoCompleteMode=AutoCompleteMode.SuggestAppend;
                txtInvoiceProductCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtInvoiceProductCode.AutoCompleteCustomSource = Collection;
                conn.Close();
        }

After that call AutoCompleteText() function within Load function in a form.

IDE Used To Test This Code : Microsoft Visual Studio.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

Post a Comment