Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Textbox Control with Autocomplete

0.00/5 (No votes)
27 May 2014 1  
WindowsForm Textbox with custom Autocomplete function

Introduction

I needed for a project a Textbox with an Autocomplete function that I can use multiple times in one Textbox and at any point in the Text, but the default Windows Forms Textbox dosen't support this function. So I created this class to provide a custom function for an Autocomplete with suggesting.

Using the Code

After adding the cTextBox class to your project and compiling, you can use the Textbox control from the Toolbar like all other controls. The control is built for the .NET Framework 4.0 and Linq but if you don't want a Linq reference in your project, you can change the following code.

Default with LINQ:

Private Function CheckInAutoCompleteCollection() As Boolean
        If myCurrentSuggest Is Nothing Then
            myCurrentSuggest = New List(Of String)
        Else
            myCurrentSuggest.Clear()
        End If

        ''Without Linq
        'For Each dat As String In MySuggestCollection
        '    If dat.ToUpper.StartsWith(strInputChars.ToUpper) Then
        '        myCurrentSuggest.Add(dat)
        '    End If
        'Next

        'With Linq
        myCurrentSuggest = MySuggestCollection.Where(Function(s) _
        s.ToUpper.StartsWith(strInputChars.ToUpper)).ToList()


        If myCurrentSuggest.Count > 0 Then
            Return True
        Else
            strInputChars = ""
            Return False
        End If
    End Function 

Without LINQ:

    Private Function CheckInAutoCompleteCollection() As Boolean
        If myCurrentSuggest Is Nothing Then
            myCurrentSuggest = New List(Of String)
        Else
            myCurrentSuggest.Clear()
        End If

        'Without Linq
        For Each dat As String In MySuggestCollection
            If dat.ToUpper.StartsWith(strInputChars.ToUpper) Then
                myCurrentSuggest.Add(dat)
            End If
        Next

        ''With Linq
        'myCurrentSuggest = MySuggestCollection.Where(Function(s) _
        s.ToUpper.StartsWith(strInputChars.ToUpper)).ToList()


        If myCurrentSuggest.Count > 0 Then
            Return True
        Else
            strInputChars = ""
            Return False
        End If
    End Function 

You can also show the Suggestlist with code or with a shortcut Key. The high for the Suggestlist is calculated from the Items it has, but it has max high before the Scrollbar is shown. As Datasource for the Autocomplete, you need a string Array, if there is no Datasource the Textbox works like the Windowsform Textbox.

AutoCompleteControlMaxHeighBeforScrolling has the default value of 100 and AutoCompleteListHotKey is set to F3, the Hotkey only works when the Textbox has the focus.

     Me.CTextBox1.AutoCompleteCollection = New String() {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
        Me.CTextBox1.AutoCompleteControlMaxHeighBeforScrolling = 100
        Me.CTextBox1.AutoCompleteListHotKey = System.Windows.Forms.Keys.F3 

For showing the Suggestlist from code, call the following function:

    ''' <summary>
    ''' Shows the Autocomplete Control with all Values
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ShowAutoCompleteControl()
        If CheckAutoCompleteActiv() Then
            UpdateSuggestListBox(True)
            mShowAutoCompleteControl()
        End If
    End Sub 

Everytime after changing the suggestion, the control raises an event:

    ''' <summary>
    ''' Event is fired after the List of current Suggest changed
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    <Description("Event is fired after the List of current Suggest changed"), _
    Category("AutoComplete")> _
    Public Event CurrentSuggestChanged(sender As Object, e As EventArgs) 

The current suggestions are shown as a Readonly String Array in the code:

    ''' <summary>
    ''' Current Suggestion for the Input Value
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Description("Readonly List of String for the Current Suggests"), _
    Category("AutoComplete")> _
    Public ReadOnly Property CurrentSuggestCollection As String()
        Get
            If myCurrentSuggest Is Nothing Then
                Return Nothing
            Else
                Return myCurrentSuggest.ToArray
            End If
        End Get
    End Property 

All values can also be changed in the Designer, the Properties have their own category "AutoComplete".

Points of Interest

This is my first posting to The Code Project. The Code Project has been a great source of help to me in my programming. I look forward to hearing your comments and suggestions.

History

  • Version 1.0 released 27.05.2014

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here