26/10/2012 by Nitesh

AutoComplete Textbox in Windows Form


We use AutoComplete feature in our websites many times. Wonder, how we can achieve the same in windows forms.

Let us build it today. We will do it step by step as mentioned below-

  1. Open Visual Studio and create a new Windows Form Application Project and name it “AutoCompleter”

  1. Drag and Add a TextBox to the form.

  1. Double Click the form to create the Form_Load() Event Handler.
private void Form1_Load(object sender, EventArgs e)
  1. Write the below code in the Event Handler.
   SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=True;Pooling=False");
   SqlCommand cmd = new SqlCommand("Select distinct [name] from [IPLTeams] order by [name] ASC", con);
   SqlDataReader re = cmd.ExecuteReader();
   AutoCompleteStringCollection coll = new AutoCompleteStringCollection();
   textBox1.AutoCompleteCustomSource = coll;
  1. Build the project and Execute.

Hope you find it useful. Let me know your thoughts via Comments.