ComboBox

A ComboBox displays a text box combined with a ListBox, which enables the user to select items from the list or enter new values. From the version Visual Studio 2005, some of the controls support Autocomplete feature including the ComboBox controls.

autocomplete combobox c# vb.net

Autocomplete ComboBox

autocomplete combobox property c# vb.net

The AutoComplete properties like AutoCompleteCustomSource, AutoCompleteMode and AutoCompleteSource to perform a TextBox that automatically completes user entry strings by comparing the initial letters being entered to the prefixes of all strings in a data source. The use of the AutoCompleteCustomSource property is optional, but you must set the AutoCompleteSource property to CustomSource in order to use AutoCompleteCustomSource like data source from database, list etc.

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.

C# Source Code

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.

VB.Net Source Code

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
NEXT.....Convert enum to List