AutoComplete TextBox In Windows Form

AutoComplete textbox is used for completing a text automatically when you type some alphabet in the textbox. It is used for searching and filtering the text in a fastest way.

Here I am explaining how to implement the autocomplete textbox in windows form.

Please design the following windows form:
 
 
Here, I have a textbox where I should type the text to search.
 
Here, I have the database table from where I searched the name that is typed in the textbox.
 
 
 

Code

  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. using System.ComponentModel;  
  5.   
  6. using System.Data;  
  7.   
  8. using System.Drawing;  
  9.   
  10. using System.Linq;  
  11.   
  12. using System.Text;  
  13.   
  14. using System.Threading.Tasks;  
  15.   
  16. using System.Windows.Forms;  
  17.   
  18. using System.Data;  
  19.   
  20. using System.Data.SqlClient;  
  21.   
  22. namespace AutoComplete  
  23.   
  24. {  
  25.   
  26.     public partial class Form1: Form  
  27.   
  28.     {  
  29.   
  30.         AutoCompleteStringCollection coll = new AutoCompleteStringCollection();  
  31.   
  32.         SqlConnection con = new SqlConnection("Data Source=TUNA-PC;Initial Catalog=Employee;User ID=sa;Password=123");  
  33.   
  34.         SqlDataAdapter da;  
  35.   
  36.         public Form1()  
  37.   
  38.         {  
  39.   
  40.             InitializeComponent();  
  41.   
  42.         }  
  43.   
  44.         private void Form1_Load(object sender, EventArgs e)  
  45.   
  46.         {  
  47.   
  48.             Auto(); //calling the auto method.  
  49.   
  50.         }  
  51.   
  52.         //Main logic for autocomplete   
  53.         public void Auto()  
  54.   
  55.         {  
  56.   
  57.             da = new SqlDataAdapter("select Name from tbl_empdetails order by name asc", con);  
  58.   
  59.             DataTable dt = new DataTable();  
  60.   
  61.             da.Fill(dt);  
  62.   
  63.             if (dt.Rows.Count > 0)  
  64.   
  65.             {  
  66.   
  67.                 for (int i = 0; i < dt.Rows.Count; i++)  
  68.   
  69.                 {  
  70.   
  71.                     coll.Add(dt.Rows[i]["Name"].ToString());  
  72.   
  73.                 }  
  74.   
  75.             } else  
  76.   
  77.             {  
  78.   
  79.                 MessageBox.Show("Name not found");  
  80.   
  81.             }  
  82.   
  83.             textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;  
  84.   
  85.             textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;  
  86.   
  87.             textBox1.AutoCompleteCustomSource = coll;  
  88.   
  89.         }  
  90.   
  91.         public void fillgrid()  
  92.   
  93.         {  
  94.   
  95.             da = new SqlDataAdapter("select * from tbl_empdetails where name='" + textBox1.Text + "'", con);  
  96.   
  97.             DataTable dt = new DataTable();  
  98.   
  99.             da.Fill(dt);  
  100.   
  101.             if (dt.Rows.Count > 0)  
  102.   
  103.             {  
  104.   
  105.                 dataGridView1.DataSource = dt;  
  106.   
  107.   
  108.             }  
  109.   
  110.         }  
  111.   
  112.         private void textBox1_TextChanged(object sender, EventArgs e)  
  113.   
  114.         {  
  115.   
  116.             fillgrid(); //when selecting the searched name then filling its data in datagrid.  
  117.   
  118.         }  
  119.   
  120.   
  121.     }  
  122.   
  123. }  

Now while running the form it will look like this.

 
 
When selecting any name, the data will be filled in the datagrid.
 
 

AutoCompleteMode property defines how text is suggested in the TextBox. It can be set to a AutoCompleteMode enumeration, Append, Suggest, SuggestAppend, and None. Suggest displays all the suggestions as dropdown. Append displays first value of the suggestion appended or selected in the TextBox, other values can be navigated using arrow keys. SuggestAppend displays suggested values as dropdown and first value appended in the TextBox.

AutoCompleteSource property sets the source for auto complete data. It can be set to a AutoCompleteSource enumeration, FileSystem, HistoryList, RecentlyUsedList, AllUrl, AlSystemSources, FileSystemDirectories, CustomSource or None. As we are getting our own data we set it to CustomSource.

Thus in this way we can create autocomplete textbox like this.

Up Next
    Ebook Download
    View all
    Learn
    View all