Click here to Skip to main content
15,887,262 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Textbox Control with Autocomplete

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
27 May 2014CPOL2 min read 9.9K   447   3  
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:

VB.NET
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:

VB.NET
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.

VB.NET
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:

VB.NET
''' <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:

VB.NET
''' <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:

VB.NET
''' <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)


Written By
Austria Austria
You can find my Blog @ dlution.at

Comments and Discussions

 
-- There are no messages in this forum --