AutoComplete Feature in Windows Form Application

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"

    create-windows-form-application.gif
     
  2. Drag and Add a TextBox to the form.

    drag-controls-on-form.gif
     
  3. Double Click the form to create the Form_Load() Event Handler.

    private void Form1_Load(object sender, EventArgs e)
    {
    }
     
  4. Write the below code in the Event Handler.

    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=True;Pooling=False");
    con.Open();
    SqlCommand cmd = new SqlCommand("Select distinct [name] from [IPLTeams] order by [name] ASC", con);
    SqlDataReader re = cmd.ExecuteReader();
    AutoCompleteStringCollection coll = new AutoCompleteStringCollection();
    while(re.Read())
    {
        coll.Add(re.GetString(0));
    }
    textBox1.AutoCompleteCustomSource = coll;
    con.Close();
     
  5. Build the project and Execute.

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

Rebin Infotech
Think. Innovate. Grow.