ARTICLE

Auto Complete TextBox in VB.NET

Posted by Mahesh Chand Articles | Windows Forms VB.NET May 24, 2010
This article demonstrates how to use auto completion text related features available in ComboBox and TextBox controls in Windows Forms using VB.NET.
Reader Level:

Auto Complete TextBox in VB.NET

Most of the developers are familiar with the auto completion text feature available in browsers, search controls and other controls. The auto completion feature is when you start typing some characters in a control; the matching items are loaded automatically for you.

In Windows Forms 2.0 or later versions (Visual Studio 2005 or later versions), some of the controls support this feature including the ComboBox and the TextBox controls. By using these features, we can build Google search like auto completion functionality in our Windows Forms applications.

For example, we can have a ComboBox control that completes URLs as soon as you type any character. For example, in Figure 1, I typed "c-s" and I see all the URLs starting with "c-s".

ACimg1.jpg
Figure 1

The AutoCompleteSource and AutoCompleteMode properties of the TextBox and ComboBox controls allow developers to provide automatic completion text feature. You can set both of these properties at design-time as well as at run-time. If you click on AutoCompleteSource drop down, you will see all the options in the drop-down list. See Figure 2.

ACImg2.jpg
Figure 2

Figure 3 shows AutoCompleteMode options.

ACImg3.jpg
Figure 3

You can also set these properties at run-time using the following code:  

comboBox1.AutoCompleteSource = AutoCompleteSource.AllSystemSources
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend

The AutoCompleteSource Enumeration has following members:

  • AllSystemResources - Specifies the equivalent of FileSystem and AllUrl as the source. This is the default value when AutoCompleteMode has been set to a value other than the default.
  • AllUrl - Specifies the equivalent of HistoryList and RecentlyUsedList as the source.
  • CustomSource - Specifies strings from a built-in AutoCompleteStringCollection as the source.
  • FileSystem - Specifies the file system as the source. 
  • FileSystemDirectories - Specifies that only directory names and not file names will be automatically completed.
  • HistoryList - Includes the Uniform Resource Locators (URLs) in the history list.
  • ListItems - Specifies that the items of the ComboBox represent the source.
  • None - Specifies that no AutoCompleteSource is currently in use. This is the default value of AutoCompleteSource.
  • RecentlyUsedList - Includes the Uniform Resource Locators (URLs) in the list of those URLs most recently used.

The AutoCompleteMode enumeration has following members:

  • Append - Appends the remainder of the most likely candidate string to the existing characters, highlighting the appended characters.
  • None - Disables the automatic completion feature for the ComboBox and TextBox controls.
  • Suggest - Displays the auxiliary drop-down list associated with the edit control. This drop-down is populated with one or more suggested completion strings.
  • SuggestAppend - Applies both Suggest and Append options.

Loading Custom Source 

We can also specify a custom source from where the listing will be loaded. If you click on the AutoCompleteCustomSource property, it will open the String Collection Editor, where we can add our strings. For example, I add following strings to the strings list. See Figure 4.

ACImg4.jpg
Figure 4

Now we need to set AutoCompleteSource to CustomSource and AutoCompleteMode to Suggest.

 

And when I run the application and type "m" in the ComboBox, I see output that looks like Figure 5. As you can see from Figure 5, all author names starting with letter m are listed in the list.

ACImg5.jpg
Figure 5

We can also create AutoCompleteStringCollection programmatically. The following code creates an AutoCompleteStringCollection, adds strings to the collection, and sets it to the AutoCompleteCustomSource of the ComboBox. 

' Create a string collection

        Dim authors As New AutoCompleteStringCollection

        authors.Add(" Mike Gold")

        authors.Add(" Praveen Kumar")

        authors.Add(" Mahesh Chand")

        authors.Add(" Michelle Ronald")

        authors.Add(" Mayor Rogers")

        authors.Add(" Raj Beniwal")

        authors.Add(" Dinesh Beniwal")

        authors.Add(" Neel Beniwal")

        authors.Add(" Nipun Tomar")

 

        ' Set ComboBox AutoComplete properties

        ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest

        ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

        ComboBox1.AutoCompleteCustomSource = authors

Summary

The AutoComplete feature of ComboBox and TextBox controls allow us to set the auto complete text feature. In this article, we saw how to use this feature in our application at design-time as well as at run-time.

Login to add your contents and source code to this article
share this article :
post comment
 

You will want to get the contents from the db first, then put it on the array programatically. For example, you may not want to get the 10,000 records from the db, so you'll refill the contents of the array after each letter press with only the starting letters. Example: the user types 'a' on the text box, then you'll refill the contents of the array with data from the db which starts with 'a', and perhaps limit the number of records to get to 100 or so. the key here is not to get too few or too much data from db.

Posted by Bangon Kali Dec 14, 2011

sir, what if i want the autocomplete textbox to the source from the database?

Posted by keith aranjuez Dec 14, 2011

hello! I'm a student of MCA. In doing my project on vb.net and in access I'm facing with many problems. I am using auto number as primary key and other fields as number in access and want to insert data through vb.net but I don't know how to use auto number for inserting data in .net can any one help me.

Posted by sangita sar Jan 05, 2011

really appreciate it.

Posted by Bangon Kali Dec 29, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor