AutoCompleteTextBox in C# Windows Form Application

In this article, we will learn how to create an AutoCompleteTextBox using a C# Windows Forms application. In my previous article, we learned How to Search Records in DataGridView Using C#.

Let's begin.

Create a new Windows Forms application.

Drop a Label and TextBox control from the ToolBox.
 
Now go to the Code Behind file (.cs code) and add the following lines of code:
  1. using System;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace AutoCompleteTextBoxDemo  
  5. {  
  6.     public partial class Form1 : Form  
  7.     {  
  8.         public Form1()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.         //AutoCompleteData Method  
  13.         private void autoCompleteData()  
  14.         {  
  15.             //Set AutoCompleteSource property of txt_StateName as CustomSource  
  16.             txt_StateName.AutoCompleteSource = AutoCompleteSource.CustomSource;  
  17.             //Set AutoCompleteMode property of txt_StateName as SuggestAppend. SuggestAppend Applies both Suggest and Append  
  18.             txt_StateName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;  
  19.             txt_StateName.AutoCompleteCustomSource.AddRange(new string[]{"Maharastra","Andhra Pradesh","Assam","Punjab","Arunachal   
  20.   
  21. Pradesh","Bihar","Goa","Gujarat","Haryana"});  
  22.         }  
  23.   
  24.         private void Form1_Load(object sender, EventArgs e)  
  25.         {  
  26.             autoCompleteData();  
  27.         }  
  28.     }  
  29. }  
In the preceding code, we set the AutoCompleteSource, AutoCompleteMode and AutoCompleteCustomSource properties of the TextBox named txt_StateName so that it automatically completes the input string.

Preview


AutoComplete TextBox with Database:

In this example, we will Suggest/Append the data in the TextBox (txt_StateName) from the database. For demonstration, I have created a database (named Sample). Add a Table, tbl_State. The following is the table schema for creating tbl_State.

Add the following lines of code:
  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Data.SqlClient;  
  4.   
  5. namespace AutoCompleteTextBoxDemo  
  6. {  
  7.     public partial class Form2 : Form  
  8.     {  
  9.         public Form2()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.         private void autoCompleteData()  
  14.         {  
  15.             SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");  
  16.             SqlCommand com = new SqlCommand("Select State from tbl_State", con);  
  17.             con.Open();  
  18.             SqlDataReader rdr = com.ExecuteReader();  
  19.             //AutoCompleteStringCollection Contains a collection of strings to use for the auto-complete feature on certain Windows Forms controls.  
  20.             AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection();  
  21.             while (rdr.Read())  
  22.             {  
  23.                 autoCompleteCollection.Add(rdr.GetString(0));  
  24.             }  
  25.             //Set AutoCompleteSource property of txt_StateName as CustomSource  
  26.             txt_StateName.AutoCompleteSource = AutoCompleteSource.CustomSource;  
  27.             //Set AutoCompleteMode property of txt_StateName as SuggestAppend. SuggestAppend Applies both Suggest and Append  
  28.             txt_StateName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;  
  29.             txt_StateName.AutoCompleteCustomSource = autoCompleteCollection;  
  30.             con.Close();  
  31.         }  
  32.         //Form2_Load Event  
  33.         private void Form2_Load(object sender, EventArgs e)  
  34.         {  
  35.             autoCompleteData();  
  36.         }  
  37.     }  
  38. }  
Preview
 
I hope you like it. Thanks.


Similar Articles