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
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
For Each dat As String In MySuggestCollection
If dat.ToUpper.StartsWith(strInputChars.ToUpper) Then
myCurrentSuggest.Add(dat)
End If
Next
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:
Public Sub ShowAutoCompleteControl()
If CheckAutoCompleteActiv() Then
UpdateSuggestListBox(True)
mShowAutoCompleteControl()
End If
End Sub Everytime after changing the suggestion, the control raises an event:
<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:
<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