Hi,
I was wondering if the solution I created below is the best way to do what I want.
I have a list that when the user types in, i'd like the suggestions to pop up, to suggest the items that begin with the text first, but then also include all other items that have that text within it below that.
i.e.
if the list is
I was wondering if the solution I created below is the best way to do what I want.
I have a list that when the user types in, i'd like the suggestions to pop up, to suggest the items that begin with the text first, but then also include all other items that have that text within it below that.
i.e.
if the list is
aper
aaper
ape
per
pants per
aaa per
and the user types 'p' then I want the list to show
pants per
per
aaa per
aaper
ape
aper
when the user types 'pe' then i want the list to show
per
aaa per
aaper
ape
aper
pants per
when the user types 'per' then i want the list to show
per
aaa per
aaper
aper
pants per
My ApplyFilterToDropDown is an almost copy of the Telerik one, but injecting my comparer instead. I had to do this because if I just set the ItemsSortComparer to mine, then called the base.ApplyFilterToDropDown it always got wiped out, so I resorted to this.
Imports
Telerik.WinControls.UI
Public
Class
Form7
Inherits
System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected
Overrides
Sub
Dispose(
ByVal
disposing
As
Boolean
)
Try
If
disposing
AndAlso
components IsNot
Nothing
Then
components.Dispose()
End
If
Finally
MyBase
.Dispose(disposing)
End
Try
End
Sub
'Required by the Windows Form Designer
Private
components
As
System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private
Sub
InitializeComponent()
Dim
RadListDataItem1
As
Telerik.WinControls.UI.RadListDataItem =
New
Telerik.WinControls.UI.RadListDataItem()
Dim
RadListDataItem2
As
Telerik.WinControls.UI.RadListDataItem =
New
Telerik.WinControls.UI.RadListDataItem()
Dim
RadListDataItem3
As
Telerik.WinControls.UI.RadListDataItem =
New
Telerik.WinControls.UI.RadListDataItem()
Dim
RadListDataItem4
As
Telerik.WinControls.UI.RadListDataItem =
New
Telerik.WinControls.UI.RadListDataItem()
Dim
RadListDataItem5
As
Telerik.WinControls.UI.RadListDataItem =
New
Telerik.WinControls.UI.RadListDataItem()
Dim
RadListDataItem6
As
Telerik.WinControls.UI.RadListDataItem =
New
Telerik.WinControls.UI.RadListDataItem()
Me
.RadDropDownList1 =
New
Telerik.WinControls.UI.RadDropDownList()
CType
(
Me
.RadDropDownList1, System.ComponentModel.ISupportInitialize).BeginInit()
Me
.SuspendLayout()
'
'RadDropDownList1
'
Me
.RadDropDownList1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend
Me
.RadDropDownList1.DropDownAnimationEnabled =
True
RadListDataItem1.Text =
"aper"
RadListDataItem1.TextWrap =
True
RadListDataItem2.Text =
"aaper"
RadListDataItem2.TextWrap =
True
RadListDataItem3.Text =
"ape"
RadListDataItem3.TextWrap =
True
RadListDataItem4.Text =
"per"
RadListDataItem4.TextWrap =
True
RadListDataItem5.Text =
"pants per"
RadListDataItem5.TextWrap =
True
RadListDataItem6.Text =
"aaa per"
RadListDataItem6.TextWrap =
True
Me
.RadDropDownList1.Items.Add(RadListDataItem1)
Me
.RadDropDownList1.Items.Add(RadListDataItem2)
Me
.RadDropDownList1.Items.Add(RadListDataItem3)
Me
.RadDropDownList1.Items.Add(RadListDataItem4)
Me
.RadDropDownList1.Items.Add(RadListDataItem5)
Me
.RadDropDownList1.Items.Add(RadListDataItem6)
Me
.RadDropDownList1.Location =
New
System.Drawing.Point(93, 126)
Me
.RadDropDownList1.Name =
"RadDropDownList1"
Me
.RadDropDownList1.ShowImageInEditorArea =
True
Me
.RadDropDownList1.Size =
New
System.Drawing.Size(106, 20)
Me
.RadDropDownList1.TabIndex = 1
Me
.RadDropDownList1.ThemeName =
"ControlDefault"
'
'Form7
'
Me
.AutoScaleDimensions =
New
System.Drawing.SizeF(6.0!, 13.0!)
Me
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me
.ClientSize =
New
System.Drawing.Size(292, 273)
Me
.Controls.Add(
Me
.RadDropDownList1)
Me
.Name =
"Form7"
Me
.Text =
"Form7"
CType
(
Me
.RadDropDownList1, System.ComponentModel.ISupportInitialize).EndInit()
Me
.ResumeLayout(
False
)
Me
.PerformLayout()
End
Sub
Friend
WithEvents
RadDropDownList1
As
Telerik.WinControls.UI.RadDropDownList
Public
Class
CustomAutoCompleteSuggestHelper
Inherits
AutoCompleteSuggestHelper
Public
Sub
New
(element
As
RadDropDownListElement)
MyBase
.
New
(element)
End
Sub
Protected
Overrides
Function
DefaultFilter(item
As
RadListDataItem)
As
Boolean
Return
item.Text.ToLower().Contains(
Me
.Filter.ToLower())
End
Function
Public
Overrides
Sub
AutoComplete(e
As
KeyPressEventArgs)
MyBase
.AutoComplete(e)
If
Me
.DropDownList.Items.Count > 0
Then
'Me.DropDownList.SelectedIndex = Me.DropDownList.FindString(Me.Filter)
End
If
End
Sub
Private
mFilter
As
String
=
String
.Empty
Protected
Overrides
ReadOnly
Property
Filter
As
String
Get
Return
mFilter
End
Get
End
Property
Public
Overrides
Sub
ApplyFilterToDropDown(filter
As
String
)
Static
filterFirstComparer
As
ListItemFilterAscendingComparer
mFilter = filter
If
String
.IsNullOrEmpty(filter)
Then
MyBase
.ApplyFilterToDropDown(filter)
Return
End
If
With
Me
.DropDownList.ListElement
.SelectionMode = SelectionMode.None
.BeginUpdate()
.Filter =
Nothing
.Filter =
AddressOf
DefaultFilter
.SortStyle = Telerik.WinControls.Enumerations.SortStyle.Ascending
End
With
' will move all items that begin with the filter, to the top of the list
If
filterFirstComparer
Is
Nothing
Then
filterFirstComparer =
New
ListItemFilterAscendingComparer
With
{.filter = filter}
Else
filterFirstComparer.filter = filter
End
If
Me
.DropDownList.ListElement.ItemsSortComparer = filterFirstComparer
Me
.DropDownList.ListElement.EndUpdate()
End
Sub
''' <summary>
''' This class is used to compare data items when sorting in ascending order.
''' </summary>
Private
Class
ListItemFilterAscendingComparer
Implements
System.Collections.Generic.IComparer(Of RadListDataItem)
Public
Property
filter
As
String
Public
Overridable
Function
Compare(x
As
RadListDataItem, y
As
RadListDataItem)
As
Integer
Implements
System.Collections.Generic.IComparer(Of Telerik.WinControls.UI.RadListDataItem).Compare
Dim
ignoreCase =
False
If
x.Owner IsNot
Nothing
Then
ignoreCase =
Not
x.Owner.CaseSensitiveSort
End
If
Dim
xStart = x.Text.StartsWith(filter, System.StringComparison.InvariantCultureIgnoreCase)
Dim
yStart = y.Text.StartsWith(filter, System.StringComparison.InvariantCultureIgnoreCase)
If
xStart
AndAlso
Not
yStart
Then
Return
-1
ElseIf
yStart
AndAlso
Not
xStart
Then
Return
1
End
If
Return
String
.Compare(x.Text, y.Text, ignoreCase)
End
Function
End
Class
End
Class
Private
Sub
Form7_Load(sender
As
Object
, e
As
System.EventArgs)
Handles
Me
.Load
RadDropDownList1.AutoCompleteMode = Windows.Forms.AutoCompleteMode.SuggestAppend
RadDropDownList1.DropDownListElement.AutoCompleteSuggest =
New
CustomAutoCompleteSuggestHelper(RadDropDownList1.DropDownListElement)
End
Sub
End
Class