Autocomplete ComboBox

A ComboBox is a graphical user interface element that seamlessly integrates a text box with a ListBox, offering users the dual functionality of selecting pre-existing items from the list or entering new values. Notably, since the release of Visual Studio 2005, certain controls have been enriched with the Autocomplete feature, and this includes the ComboBox controls. The Autocomplete feature significantly enhances the user experience by providing predictive suggestions as the user types in the ComboBox, saving time and reducing errors during data entry or selection.

Autocomplete ComboBox

autocomplete combobox c# vb.net

The AutoComplete properties, namely AutoCompleteCustomSource, AutoCompleteMode, and AutoCompleteSource, enable the functionality of a TextBox that automatically completes user entry strings by intelligently comparing the initial letters being entered to the prefixes of all strings in a designated data source. While the utilization of the AutoCompleteCustomSource property is discretionary, it is essential to set the AutoCompleteSource property to CustomSource to utilize the power of AutoCompleteCustomSource as a data source, which can encompass databases, lists, or other relevant sources.

In simpler terms, these AutoComplete properties facilitate the implementation of a TextBox with an automatic suggestion feature that predicts and completes user input based on the initial characters entered. If desired, developers can provide a custom set of suggestions using the AutoCompleteCustomSource property, but it is crucial to indicate CustomSource in the AutoCompleteSource property to enable the use of this custom data source effectively.

C# Autocomplete ComboBox

autocomplete combobox vb.net

The following program displays a C# Autocomplete ComboBox that select the values from Database and add to AutoCompleteStringCollection of the Autocomplete ComboBox.


Source Code | C#
using System; using System.Data; using System.Data.SqlClient ; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest; comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; AutoCompleteStringCollection combData = new AutoCompleteStringCollection(); getData(combData); comboBox1.AutoCompleteCustomSource = combData; } private void getData(AutoCompleteStringCollection dataCollection) { string connetionString = null; SqlConnection connection; SqlCommand command; SqlDataAdapter adapter = new SqlDataAdapter(); DataSet ds = new DataSet(); connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;password=zen412"; string sql = "SELECT DISTINCT [fname] FROM [employee]"; connection = new SqlConnection(connetionString); try { connection.Open(); command = new SqlCommand(sql, connection); adapter.SelectCommand = command; adapter.Fill(ds); adapter.Dispose(); command.Dispose(); connection.Close(); foreach (DataRow row in ds.Tables[0].Rows) { dataCollection.Add(row[0].ToString()); } } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); } } } }

VB.Net Autocomplete ComboBox

autocomplete combobox c#

The following program displays a VB.Net Autocomplete ComboBox that select the values from Database and add to AutoCompleteStringCollection of the Autocomplete ComboBox.


Source Code | VB.Net
Imports System.Data Imports System.Data.SqlClient Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource Dim combData As New AutoCompleteStringCollection() getData(combData) ComboBox1.AutoCompleteCustomSource = combData End Sub Private Sub getData(ByVal dataCollection As AutoCompleteStringCollection) Dim connetionString As String = Nothing Dim connection As SqlConnection Dim command As SqlCommand Dim adapter As New SqlDataAdapter() Dim ds As New DataSet() connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;password=zen412" Dim sql As String = "SELECT DISTINCT [fname] FROM [employee]" connection = New SqlConnection(connetionString) Try connection.Open() command = New SqlCommand(sql, connection) adapter.SelectCommand = command adapter.Fill(ds) adapter.Dispose() command.Dispose() connection.Close() For Each row As DataRow In ds.Tables(0).Rows dataCollection.Add(row(0).ToString()) Next Catch ex As Exception MessageBox.Show("Can not open connection ! ") End Try End Sub End Class