Complete TextBox in VB.NET
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.
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".
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.
shows AutoCompleteMode options.
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:
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
Specifies the equivalent of HistoryList and RecentlyUsedList as the source.
Specifies strings from a built-in AutoCompleteStringCollection as the
Specifies the file system as the source.
Specifies that only directory names and not file names will be automatically
Includes the Uniform Resource Locators (URLs) in the history list.
Specifies that the items of the ComboBox represent
Specifies that no AutoCompleteSource is
currently in use. This is the default value of AutoCompleteSource.
Includes the Uniform Resource Locators (URLs) in the list of those URLs most
The AutoCompleteMode enumeration
has following members:
Appends the remainder of the most likely candidate string to the existing
characters, highlighting the appended characters.
Disables the automatic completion feature for the ComboBox and TextBox controls.
Displays the auxiliary drop-down list associated with the edit control. This
drop-down is populated with one or more suggested completion strings.
Applies both Suggest and Append options.
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.
need to set AutoCompleteSource to CustomSource and AutoCompleteMode to Suggest.
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.
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
Set ComboBox AutoComplete properties
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.