Creating AutoComplete TextBox in Windows Froms using C#

Amit.jain
Posted by in C# category on for Intermediate level | Views : 150456 red flag

In this example i am explaining how to create AutoComplete TextBox or ComboBox in winforms using C#


 Download source code for Creating AutoComplete TextBox in Windows Froms using C#

AutoComplete TextBox or ComboBox in Winforms 
In my previous article Implementing autocomplete textbox in gridview using Ajax autocomplete extender i explained how to create AutoComplete TextBox in ASP .NET using AJAX , in this example i am achieving the same in Windows forms application
For this i've created a database with a table containing names which will be shown in textbox as suggestions, for this we need to create a AutoCompleteStringCollection and then add the records in this collection using datareader to fetch records from database

Properties of TextBox that needs to be set
For autocomplete functionalty to work we need to define these 3 properties of textbox

1. AutoCompleteMode - we can choose either suggest or appned or suggestappend as names are self explanatory

2. AutoCompleteSource - this needs to be set as Custom Source

3. AutoCompleteCustomSource - this is the collection we created earlier
C# Code
The complete C# code will look like this
namespace AutoCompleteTextBox
{

public partial class frmAuto : Form
{
public string strConnection = ConfigurationManager.AppSettings["ConnString"];
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
public frmAuto()
{
InitializeComponent();
}

private void frmAuto_Load(object sender, EventArgs e)
{
SqlDataReader dReader;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = strConnection;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select distinct [Name] from [Names] order by [Name] asc";
conn.Open();
dReader = cmd.ExecuteReader();
if (dReader.HasRows == true)
{
while (dReader.Read())
namesCollection.Add(dReader["Name"].ToString());

}
else
{
MessageBox.Show("Data not found");
}
dReader.Close();

txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtName.AutoCompleteCustomSource = namesCollection;

}

private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void btnOk_Click(object sender, EventArgs e)
{
MessageBox.Show("Hope you like this example");
}

}
}
  In the similar way we can also create a autocomplete type combobox

Page copy protected against web site content infringement by Copyscape

About the Author

Amit.jain
Full Name: amiT jaiN
Member Level: Starter
Member Status: Member
Member Since: 12/26/2008 7:19:35 AM
Country:

http://csharpdotnetfreak.blogspot.com/

Login to vote for this post.

Comments or Responses

Posted by: Sudheep.grandhe on: 12/19/2011 | Points: 25
Hi,

how can we create autocompletecustomsource.if i am creating like this.marsweb,sudheep,pankaj it shows in the textbox like this format.how can i get single username.when i enter m means it should display marsweb.
Posted by: Eldoraman on: 8/13/2014 | Points: 25
Try this one..a complete tutorial on autocomplete textbox from database

http://net-informations.com/q/faq/autocomplete.html

eldo

Login to post response

Comment using Facebook(Author doesn't get notification)