Click here to Skip to main content
Click here to Skip to main content

Textbox Control with Autocomplete

, 27 May 2014
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

You can find my Blog @ dlution.at
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 27 May 2014
Article Copyright 2014 by Taucher Christoph
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid