How to create AutoComplete Textbox in Windows Forms Application using C#.NET

How to create AutoComplete Textbox in Windows Forms Application using C#.NET

I’ll show you how to create AutoComplete Textbox in a Windows Forms Application

This time we’re going to get the data from MS Access Database
so let’s get starting!

1- Create Windows Forms Application.
2- Add Textbox Control.
3- Copy the below code into your form.

AutoCompleteStringCollection stringCollection = new AutoCompleteStringCollection();

private void AutoCompleteTextBox()
{
  OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Test.mdb");
  string con = "select distinct Question from Questions order by Question asc ";
  OleDbCommand aCommand = new OleDbCommand(con, aConnection);

  aConnection.Open();
  OleDbDataReader aReader = aCommand.ExecuteReader();

  if (aReader.HasRows)
  {
    while (aReader.Read())
    {
      stringCollection.Add(aReader[0].ToString());
    }
  }

  aReader.Close();
  aConnection.Close();

  textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
  textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
  textBox1.AutoCompleteCustomSource = stringCollection;
}

private void Form1_Load(object sender, EventArgs e)
{
	AutoCompleteTextBox();
}

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *