---
title: "Text Box Auto Complete Feature"  
description: "Unlike List Box control we can achieve AutoComplete Text Feature in Textbox. In this blog I have discussed how to achieve Autosuggest Feature of Textb"  
author: "Shankar M"  
published: 2013-02-12  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/442/text-box-auto-complete-feature  
category: "windows controls"  
tags: ["windows controls"]  
reading_time: 2 minutes  

---

# Text Box Auto Complete Feature

Unlike List Box control we can achieve [AutoComplete](https://www.mindstick.com/forum/156169/what-is-google-suggest-or-autocomplete) Text Feature in Textbox. In this blog I have discussed how to achieve Autosuggest Feature of Textbox in Windows Forms.

1. Place a [Textbox Control](https://www.mindstick.com/articles/320/textbox-control-in-vb-dot-net) on to the Form.

2. Write the code in the Form Loads Event

```
private void Form1_Load(object sender, EventArgs e)        {            SqlConnection conn;            SqlCommand cmd;            string ConnectionString = @"Data Source = L04WSCHE0471\SQLEXPRESS;Initial Catalog = ORCD;User id =yourdbUserName; Password =yourPassword; Integrated Security = True";            string CommandString = "SELECT ENAME FROM EMP";              using (conn = new SqlConnection(ConnectionString))            {               cmd = new SqlCommand(CommandString, conn);               conn.Open();               SqlDataReader myReader = cmd.ExecuteReader();               AutoCompleteStringCollection Collection = new AutoCompleteStringCollection();               while (myReader.Read())               {                   Collection.Add(myReader["ENAME"].ToString());               }               textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;               textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;               textBox1.AutoCompleteCustomSource = Collection;            }        }
```

Here, we create an instance of [SqlConnection](https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable) and [SqlCommand](https://www.mindstick.com/forum/91/difference-between-sqlcommand-and-sqlcommandbuilder) Class. Then, we pass the SqlCommand Class the SQL Statement.

Once the [connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) is opened based on the command passed, we create a [DataReader](https://www.mindstick.com/forum/160514/how-to-use-datareader-in-asp-dot-net-c-sharp).

Note, that the DataReader requires a live connection to the database.

To read the [data from the database](https://www.mindstick.com/forum/160510/how-to-fetch-data-from-the-database-in-the-dot-net-console-application-using-c-sharp), we Iterate using the myReader.Read () method. After the AutoCompleteStringCollection object is built we assign it to the textbox AutoCompleteCustomSource [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) which is set to [enumeration](https://www.mindstick.com/blog/88/enumeration-in-c-sharp) AutoCompleteSource.CustomSource. The textbox AutoCompleteMode property is set to enumeration AutoCompleteMode.SuggestAppend.

![Text Box Auto Complete Feature](http://www.flickr.com/photos/93160469@N03/8469312827/in/photostream)

Thanks for reading..

---

Original Source: https://www.mindstick.com/blog/442/text-box-auto-complete-feature

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
