Sabari M Sabari M
Updated date Mar 16, 2021
In this article, we will see how to enable the auto-complete feature for TextBox and ComboBox in the C# windows application. The auto-complete feature enables the user to select and capture the items or records easily by entering few characters in the TextBox or ComboBox.

Download File(s):

AutoCompleteTextBox.rar

In this article, we will see how to enable the auto-complete feature for TextBox and ComboBox in the C# windows application. The auto-complete feature enables the user to select and capture the items or records easily by entering few characters in the TextBox or ComboBox.

Let's start to discuss how to enable this feature for TextBox and ComboBox using C#.

Step 1:

Open Visual Studio 2019 and click "Create a new project" and choose Windows Forms App (.Net Framework) > Provide project name and location and click "Create"

Step 2:

Add database and table in MSSQL server. Add columns as shown below,

Step 3:

Add controls (Labels, Textbox, and Combobox) to the form as shown below,

Step 4: Enable auto-complete feature for TextBox

Next, Let's make a connection and enable the auto-complete feature for TextBox. Add the below code to enable the feature.

// Sql connection
SqlConnection con = new SqlConnection("Data Source=LAPTOP-4EPDJLBG\\SQLExpress; Initial Catalog = CarModelsDb; User Id =sa; Password=sa;");

public Form1()
{
	InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    // Populate data for textbox
	PopulateDataForTextBox();    
}

// Load data from database
private void PopulateDataForTextBox()
{   
	SqlCommand com = new SqlCommand("select country from tblCountry", con);            
	// Open the connection
	con.Open();
	SqlDataReader reader = com.ExecuteReader();

	// Add AutoCompleteStringCollection to enable auto-complete feature for Windows Forms controls.
	AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection();
	while (reader.Read())
	{
		autoCompleteCollection.Add(reader.GetString(0).Trim());
	}
	// Set AutoCompleteSource property to CustomSource for the control txtCountry
	txtCountry.AutoCompleteSource = AutoCompleteSource.CustomSource;
	// Set AutoCompleteMode property to SuggestAppend for the control txtCountry
	txtCountry.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
	txtCountry.AutoCompleteCustomSource = autoCompleteCollection;
	// Close the connection
	con.Close();
}

Step 4: Enable auto-complete feature for ComboBox

To enable the auto-complete feature for Combobox, add the below code,

// Load data from database
private void PopulateDataForComboBox()
{
	SqlCommand com = new SqlCommand("select country from tblCountry", con);
	com.CommandType = CommandType.Text;
	// Open connection
	con.Open();
	// Fill data
	SqlDataAdapter dataAdapter = new SqlDataAdapter();
	DataTable dataTable = new DataTable();
	dataAdapter.SelectCommand = com;
	dataAdapter.Fill(dataTable);         
	// Bind data to the ComboBox
	cboAutoCompleteCountry.DataSource = dataTable;
	cboAutoCompleteCountry.DisplayMember = "Country";
	// Close connection
	con.Close();
}

Screenshots:

Auto-complete TextBox:

Auto-complete ComboBox:

 

ABOUT THE AUTHOR

Sabari M
Sabari M
Software Professional, India

IT professional with 14+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!