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

Comments (12)

  • Sharon Whitman Reply

    Way cool! Some very valid points! I appreciate you penning this post and the rest of the site is really good.

    January 12, 2015 at 9:19 PM
  • Jimmy Wrådhe Reply

    is that type possible to do with a active directory connection instead?

    November 30, 2021 at 2:22 AM
    • Amr Saafan

      This is a good question.
      As you can see TextBox has property named AutoCompleteCustomSource.
      In this example it takes StringCollection as a datasource.
      Therefore, what we need to do is connect to the Active Directory, query whatever data we want and add it to our StringCollection to pass it as a datasource to the TextBox.
      I have another blog post is talking about How to get Active Directory connection string in C#.NET maybe this will help.

      Let me know if you need any other help

      November 30, 2021 at 9:54 AM
  • Jimmy Wrådhe Reply

    Hi!

    I am pretty new in C# and keeps learning.

    i dont really get it to work as i want!

    private void AutoCompleteTextBox_ReferenceUser()
    {

    var attributeName = “cn”;
    var searchString = textBox_referenceuser.Text;
    var ent = new DirectoryEntry(“LDAP://OU=Users,OU=Oriola,DC=se,DC=ad,DC=oriola”);
    var mySearcher = new DirectorySearcher(ent);
    mySearcher.Filter = string.Format(“(&(objectCategory=person)(objectClass=user))”, attributeName, searchString);
    mySearcher.PropertiesToLoad.Add(“name”);
    mySearcher.PropertiesToLoad.Add(“sn”);
    mySearcher.PropertiesToLoad.Add(“displayname”);

    var userResult = mySearcher.FindAll();

    if (userResult != null)
    {
    for (int i = 0; i < userResult.Count; i++)
    {
    SearchResult result = userResult[i];
    stringCollection.Add(userResult[i]);
    }
    }

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

    December 1, 2021 at 6:24 AM
    • Amr Saafan

      Please debug and make sure that StringCollection already has some data, maybe it is empty and this is what causing the troubles

      December 1, 2021 at 6:50 AM
  • Jimmy Wrådhe Reply

    Right now it looks like this row is causing this:

    stringCollection.Add(userResult[i]);

    Error CS1503 Argument 1: cannot convert from ‘System.DirectoryServices.SearchResult’ to ‘string’

    as i said, newbie…

    December 1, 2021 at 6:58 AM
    • Amr Saafan

      try this:
      userResult[i].ToString()

      December 1, 2021 at 8:15 AM
    • Jimmy Wrådhe

      yeah, no error, but no result on search in that field either :/

      December 1, 2021 at 8:34 AM
    • Amr Saafan

      Try another field that has some data

      December 1, 2021 at 10:17 AM
    • Jimmy Wrådhe

      I even tried with a variable “gl” so that it will start find for example Glenn.

      private void AutoCompleteTextBox_ReferenceUser()
      {

      var attributeName = “cn”;
      var searchString = “gl”; //textBox_referenceuser.Text;
      var ent = new DirectoryEntry(“LDAP://OU=Users,OU=Oriola,DC=se,DC=ad,DC=oriola”);
      var mySearcher = new DirectorySearcher(ent);
      mySearcher.Filter = string.Format(“(&(objectCategory=person)(objectClass=user))”, attributeName, searchString);
      mySearcher.PropertiesToLoad.Add(“name”);
      mySearcher.PropertiesToLoad.Add(“sn”);
      mySearcher.PropertiesToLoad.Add(“displayname”);

      SearchResultCollection userResult = mySearcher.FindAll();

      if (userResult != null)
      {
      for (int i = 0; i < userResult.Count; i++)
      {
      stringCollection.Add(userResult[i].ToString());
      }
      }

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

      December 1, 2021 at 3:49 PM
  • Jimmy Wrådhe Reply

    It looks like i solved it now, i tried another way to do it…

    private void AutoCompleteTextBox_ReferenceUser()
    {

    try
    {
    string displayName = string.Empty;
    var attributeName = “cn”;
    string OU = “DC = local,DC = domain”;
    var searchString = textBox_referenceuser.Text;
    var ent = new DirectoryEntry(“LDAP://”+ OU);
    var mySearcher = new DirectorySearcher(ent);
    mySearcher.Filter = string.Format(“(&(objectCategory=person)(objectClass=user))”, attributeName, searchString);

    SearchResultCollection result = mySearcher.FindAll();

    List names = new List();

    foreach(SearchResult sr in result)
    {
    var n = sr.Properties[“cn”][0].ToString();
    if (!names.Contains(n))
    {
    stringCollection.Add(n);
    }
    }

    textBox_referenceuser.AutoCompleteMode = AutoCompleteMode.Suggest;
    textBox_referenceuser.AutoCompleteSource = AutoCompleteSource.CustomSource;
    textBox_referenceuser.AutoCompleteCustomSource = stringCollection;
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

    thanks for help!

    December 1, 2021 at 5:04 PM
    • Amr Saafan

      I am always happy to help 🙂

      December 2, 2021 at 2:09 AM

Leave a Reply

Your email address will not be published.