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. namespace AutoCompleteTextBoxDemo
  4. {
  5. public partial class Form1 : Form
  6. {
  7. public Form1()
  8. {
  9. InitializeComponent();
  10. }
  11. //AutoCompleteData Method
  12. private void autoCompleteData()
  13. {
  14. //Set AutoCompleteSource property of txt_StateName as CustomSource
  15. txt_StateName.AutoCompleteSource = AutoCompleteSource.CustomSource;
  16. //Set AutoCompleteMode property of txt_StateName as SuggestAppend. SuggestAppend Applies both Suggest and Append
  17. txt_StateName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
  18. txt_StateName.AutoCompleteCustomSource.AddRange(new string[]{"Maharastra","Andhra Pradesh","Assam","Punjab","Arunachal
  19. Pradesh","Bihar","Goa","Gujarat","Haryana"});
  20. }
  21. private void Form1_Load(object sender, EventArgs e)
  22. {
  23. autoCompleteData();
  24. }
  25. }
  26. }
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. namespace AutoCompleteTextBoxDemo
  5. {
  6. public partial class Form2 : Form
  7. {
  8. public Form2()
  9. {
  10. InitializeComponent();
  11. }
  12. private void autoCompleteData()
  13. {
  14. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");
  15. SqlCommand com = new SqlCommand("Select State from tbl_State", con);
  16. con.Open();
  17. SqlDataReader rdr = com.ExecuteReader();
  18. //AutoCompleteStringCollection Contains a collection of strings to use for the auto-complete feature on certain Windows Forms controls.
  19. AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection();
  20. while (rdr.Read())
  21. {
  22. autoCompleteCollection.Add(rdr.GetString(0));
  23. }
  24. //Set AutoCompleteSource property of txt_StateName as CustomSource
  25. txt_StateName.AutoCompleteSource = AutoCompleteSource.CustomSource;
  26. //Set AutoCompleteMode property of txt_StateName as SuggestAppend. SuggestAppend Applies both Suggest and Append
  27. txt_StateName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
  28. txt_StateName.AutoCompleteCustomSource = autoCompleteCollection;
  29. con.Close();
  30. }
  31. //Form2_Load Event
  32. private void Form2_Load(object sender, EventArgs e)
  33. {
  34. autoCompleteData();
  35. }
  36. }
  37. }
Preview
I hope you like it. Thanks.