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(); }
Comments (12)
Way cool! Some very valid points! I appreciate you penning this post and the rest of the site is really good.
is that type possible to do with a active directory connection instead?
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
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;
}
Please debug and make sure that StringCollection already has some data, maybe it is empty and this is what causing the troubles
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…
try this:
userResult[i].ToString()
yeah, no error, but no result on search in that field either :/
Try another field that has some data
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;
}
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!
I am always happy to help 🙂