Autocomplete TextBox

arrow keys

From the version Visual Studio 2005, some of the controls support Autocomplete feature including the TextBox controls. The properties like AutoCompleteCustomSource, AutoCompleteMode and AutoCompleteSource to perform a TextBox that automatically completes user input strings by comparing the prefix letters being entered to the prefixes of all strings in a data source.

AutoCompleteCustomSource

AutoCompleteCustomSource is helpful for TextBox controls in which string sources like URLs, addresses etc. will be frequently entered.

AutoCompleteCustomSource property is perform to set the custom list of values.

AutoCompleteMode property is perform to set how the auto-complete candidates are displayed.

C# Autocomplete TextBox

The following C# program add some data values to AutoCompleteStringCollection and perform as an Autocomplete TextBox

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
            addItems(DataCollection);
            textBox1.AutoCompleteCustomSource = DataCollection;
        }
        public void addItems(AutoCompleteStringCollection col)
        {
            col.Add("Abel");
            col.Add("Bing");
            col.Add("Catherine");
            col.Add("Varghese");
            col.Add("John");
            col.Add("Kerry");
        }
    }
}

Autocomplete TextBox working with Database values C#

The following C# program connect to database and add Dataset values to AutoCompleteStringCollection and perform as an Autocomplete TextBox

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
            getData(DataCollection);
            textBox1.AutoCompleteCustomSource = DataCollection;
        }
        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 TextBox

Autocomplete TextBox working with Database

The following VB.Net program add some string values to AutoCompleteStringCollection and display as Autocomplete TextBox while entering text

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
        TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
        Dim DataCollection As New AutoCompleteStringCollection()
        addItems(DataCollection)
        TextBox1.AutoCompleteCustomSource = DataCollection
    End Sub
    Public Sub addItems(ByVal col As AutoCompleteStringCollection)
        col.Add("Abel")
        col.Add("Bing")
        col.Add("Catherine")
        col.Add("Varghese")
        col.Add("John")
        col.Add("Kerry")
    End Sub
End Class

Autocomplete TextBox working with Database values VB.Net

TextBox working with Database

The following VB.Net program connect to database and add Dataset values to AutoCompleteStringCollection and display as an Autocomplete TextBox while entering values to TextBox control.

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
        TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
        Dim DataCollection As New AutoCompleteStringCollection()
        getData(DataCollection)
        TextBox1.AutoCompleteCustomSource = DataCollection
    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

TextBox

A TextBox control is used to enter text in an application. It has additional capabilities that is not found in the standard Windows text box control, including multiline editing and password character masking. More about.... C# TextBox and VB.Net TextBox

Create a Numeric Text Box

In many situations you need to enter only numeric values in the Textbox. Here you can see some useful techniques that accept only numbers in the textbox. More about.... Numeric Only Textbox

NEXT.....Autocomplete ComboBox