Working with AutoComplete in Windows Forms AutoComplete

8 Jul 20269 minutes to read

This section explains how to work with various options in the AutoComplete component.

Setting AutoComplete modes

The AutoComplete component provides the auto-complete support to the editor control based on the input text. You can enable a specific mode using the SetAutoComplete method.

The different types of AutoComplete modes are as follows.

AutoComplete modes Description
AutoSuggest Suggests and displays a list of probable matches in the drop-down list by setting the AutoCompleteMode to AutoSuggest.
Windows Forms AutoComplete AutoSuggest mode
AutoAppend Appends the most appropriate match for the current content in the editor control automatically.
Windows Forms AutoComplete AutoAppend mode
Both Activates both AutoAppend and AutoSuggest modes of auto completion for the editor control.
Windows Forms AutoComplete Both mode
Disabled Disables auto complete support for the editor control.
Windows Forms AutoComplete Disabled mode
MultiSuggest Checks whether the beginning of items in the list pop up matches with user input string. Then matched cases from various columns are shown as suggestions. The MultiSuggest mode is an extended mode of AutoSuggest.
Windows Forms AutoComplete Multisuggest mode
MultiSuggestExtend Checks whether the entered character or sequence of character is present in any part of the word in list popup item. Then matched cases from various columns are shown as suggestions.
Windows Forms AutoComplete Multisuggestextended mode

The following code snippet shows how to update the AutoComplete modes.

autoComplete1.SetAutoComplete(this.textBox1, Syncfusion.Windows.Forms.Tools.AutoCompleteModes.MultiSuggestExtended);
autoComplete1.SetAutoComplete(Me.textBox1, Syncfusion.Windows.Forms.Tools.AutoCompleteModes.MultiSuggestExtended)

NOTE

The values are filtered from the column which is set as matching column for auto complete. By default, the MatchingColumn value is true for the first column of binding source.

A sample that demonstrates the AutoComplete mode is available here

Case sensitivity

Specifies whether the string comparison is case-sensitive. When the CaseSensitive property is set to true, comparisons are case-sensitive; when set to false, case is ignored. The default value of this property is true.

autoComplete1.CaseSensitive = true;
autoComplete1.CaseSensitive = True

Match mode

The MatchMode property specifies the matching algorithm used to find the most appropriate match for the current content in the editor control from the AutoComplete history list. The default value is Automatic.

The following code snippet implements column configuration.

this.autoComplete1.MatchMode = AutoCompleteMatchModes.Automatic;
Me.autoComplete1.MatchMode = AutoCompleteMatchModes.Automatic

Matching column

The MatchingColumn property indicates whether the column it represents should be treated as the matching column. The default value of this property is true for the column at the 0th index.

Refresh the columns before setting MatchingColumn by calling the RefreshColumns method.

this.autoComplete1.RefreshColumns();
this.autoComplete1.Columns[1].MatchingColumn = true;
Me.autoComplete1.RefreshColumns()
    Me.autoComplete1.Columns(1).MatchingColumn = true

Sorting items

Specifies whether sorting is performed on the items displayed in the AutoComplete popup. Sorting can be enabled by setting the AutoSortList property to true. The default value of this property is true.

autoComplete1.AutoSortList = true;
autoComplete1.AutoSortList = True

NOTE

The items are sorted on the basis of the column whose MatchingColumn are set to true.

Handling duplicate values

Duplicate values are allowed in the AutoComplete data source when the EnableDuplicateValues property is set to true. The default value is false.

autoComplete1.EnableDuplicateValues = true;
autoComplete1.EnableDuplicateValues = True

Maintaining history of user inputs

Adding items to history list

The current input text in the editor control can be added to the history list at runtime when the Enter key is pressed. This support can be enabled by setting the AutoAddItem property to true. The default value of this property is false.

autoComplete1.AutoAddItem = true;
autoComplete1.AutoAddItem = True

Deleting items from history list

The currently selected item can be removed from the AutoComplete popup when the Delete key is pressed at runtime. This support can be enabled by setting the AllowListDelete property to true.

autoComplete1.AllowListDelete = true;
autoComplete1.AllowListDelete = True

Deleting history

The history items persisted by the AutoComplete component can be deleted by invoking the ResetHistory method. The entire history list in the AutoComplete popup is cleared.

autoComplete1.ResetHistory();
autoComplete1.ResetHistory();

Setting maximum number of suggestions

You can limit the number of suggestions displayed in the AutoComplete popup using the MaxNumberOfSuggestions property.

Windows Forms AutoComplete Maximum number of suggestions

autoComplete1.MaxNumberofSuggestion = 2;
autoComplete1.MaxNumberofSuggestion = 2

Integration with MS ComboBox

If MS ComboBox is used as editor control, the Combobox dropdown can be suppressed and overridden by the AutoComplete component using the OverrideCombo property.

Windows Forms AutoComplete Integration with MS Combobox

A sample that demonstrates the above feature is available here

Integration with RichTextBox control

The auto-complete functionality can be added to a RichTextBox control. The following steps are used to integrate the RichTextBox with the AutoComplete component:

  1. Implement the IEditControlsEmbed interface in a CustomRichTextBox class that enables the AutoComplete functionality for the RichTextBox control.

    public class CustomRichTextBox : System.Windows.Forms.RichTextBox, IEditControlsEmbed
       
         {
               // Returns the active RichTextBox control.
                public Control GetActiveEditControl(IEditControlsEmbedListener listener)
                {
                           return (Control)this;
                }
         }
    Public Class CustomRichTextBox Inherits System.Windows.Forms.RichTextBox Implements IEditControlsEmbed
         ' Returns the active RichTextBox control.
         Public Function GetActiveEditControl(ByVal listener As IEditControlsEmbedListener) As Control
       
             Return CType(Me, Control)
       
         End Function
       
         End Class
  2. Create an instance for the CustomRichTextBox class and the AutoComplete component. Then, use the SetAutoComplete method of AutoComplete component to enable auto-complete support for the RichTextBox control.

    Syncfusion.Windows.Forms.Tools.AutoComplete autoComplete1= new Syncfusion.Windows.Forms.Tools.AutoComplete();
        CustomRichTextBox richTextBox1= new CustomRichTextBox();    
        autoComplete1.SetAutoComplete(richTextBox1, Syncfusion.Windows.Forms.Tools.AutoCompleteModes.AutoSuggest);
    Dim autoComplete1 As Syncfusion.Windows.Forms.Tools.AutoComplete = New Syncfusion.Windows.Forms.Tools.AutoComplete 
        Dim richTextBox1 As CustomRichTextBox = New CustomRichTextBox
        autoComplete1.SetAutoComplete(richTextBox1, AutoCompleteModes.AutoSuggest)

Windows Forms AutoComplete integration with Richtextbox

A sample that demonstrates the integration with RichTextBox control is available here

Opening the AutoComplete popup programmatically

The AutoComplete popup can be shown programmatically by setting the parent control and calling ShowPopup on the AutoCompletePopup property.

autoComplete1.AutoCompletePopup.ParentControl = textBox1;
    this.autoComplete1.AutoCompletePopup.ShowPopup(Point.Empty);
autoComplete1.AutoCompletePopup.ParentControl = textBox1

    autoComplete1.AutoCompletePopup.ShowPopup(Point.Empty)

Windows Forms AutoComplete programmatical popup open