TextBox.AutoCompleteSource Property

Definition

Gets or sets a value specifying the source of complete strings used for automatic completion.

C#
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TextBoxAutoCompleteSourceConverter))]
public System.Windows.Forms.AutoCompleteSource AutoCompleteSource { get; set; }

Property Value

One of the values of AutoCompleteSource. The options are AllSystemSources, AllUrl, FileSystem, HistoryList, RecentlyUsedList, CustomSource, and None. The default is None.

Attributes

Exceptions

The specified value is not one of the values of AutoCompleteSource.

Examples

The following code example demonstrates how to use a collection as the auto-complete custom source for a TextBox control. This example does the following:

C#
private void Form1_Load(object sender, EventArgs e)
{
    // Create the list to use as the custom source. 
    var source = new AutoCompleteStringCollection();
    source.AddRange(new string[]
                    {
                        "January",
                        "February",
                        "March",
                        "April",
                        "May",
                        "June",
                        "July",
                        "August",
                        "September",
                        "October",
                        "November",
                        "December"
                    });

    // Create and initialize the text box.
    var textBox = new TextBox
                  {
                      AutoCompleteCustomSource = source,
                      AutoCompleteMode = 
                          AutoCompleteMode.SuggestAppend,
                      AutoCompleteSource =
                          AutoCompleteSource.CustomSource,
                      Location = new Point(20, 20),
                      Width = ClientRectangle.Width - 40,
                      Visible = true
                  };

    // Add the text box to the form.
    Controls.Add(textBox);
}

Remarks

Use the AutoCompleteCustomSource, AutoCompleteMode, and AutoCompleteSource properties to create a TextBox that automatically completes input strings by comparing the prefix being entered to the prefixes of all strings in a maintained source. This is useful for TextBox controls in which URLs, addresses, file names, or commands will be frequently entered.

The use of the AutoCompleteCustomSource property is optional, but you must set the AutoCompleteSource property to CustomSource in order to use AutoCompleteCustomSource.

You must use the AutoCompleteMode and AutoCompleteSource properties together.

Note

AutoCompleteSource does not work on multiline TextBox controls.

Note

The operating system might limit the number of custom strings that can be displayed at once.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also