Autocomplete TextBox

Since the release of Visual Studio 2005, an array of controls has been integrated with the highly efficient Autocomplete feature, with TextBox controls being one of the beneficiaries. This Autocomplete functionality empowers developers with three significant properties: AutoCompleteCustomSource, AutoCompleteMode, and AutoCompleteSource. These properties collectively enable the implementation of a TextBox that effortlessly predicts and completes user input strings by intelligently comparing the entered prefix letters to the prefixes found within a designated data source.

With the AutoCompleteCustomSource property, developers gain the ability to define a custom set of options that the Autocomplete feature utilizes to offer predictive suggestions to users as they type. This empowers developers to tailor the Autocomplete functionality to suit specific application requirements, ensuring a more personalized and seamless user experience.

Autocomplete TextBox

arrow keys

The AutoCompleteMode property enables developers to precisely control the behavior of the Autocomplete feature. By selecting the appropriate mode, developers can dictate when and how the Autocomplete suggestions appear, optimizing the feature's responsiveness and adaptability to different user interaction patterns.

AutoCompleteCustomSource

The AutoCompleteCustomSource property proves to be particularly beneficial when working with TextBox controls that frequently involve the input of specific string sources, such as URLs, addresses, and other frequently used data. By utilizing this property, developers can establish a custom list of values, tailored to the unique needs of the application. This allows the Autocomplete feature to present relevant suggestions based on the user's input, streamlining the process of data entry and ensuring accuracy by offering pre-defined options that match the specific context.

On the other hand, the AutoCompleteMode property plays a vital role in determining how the auto-complete candidates are displayed to the user. This property empowers developers to choose from a variety of display modes, enabling them to control when and how the Autocomplete suggestions appear to the user as they type in the TextBox control. By selecting an appropriate mode, developers can fine-tune the user experience to best suit the application's requirements and the preferences of its users.

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

Conclusion

The AutoCompleteCustomSource property enables the setup of a custom list of values, which is particularly useful when dealing with frequently entered string sources. Meanwhile, the AutoCompleteMode property provides the flexibility to customize how the Autocomplete suggestions are presented, enhancing the user's interaction with the application and making the process of data entry more efficient and user-friendly.