[RESOLVED] how to search in a combobox
I've a combobox to display names:
Code:
Dim names As New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
names.Add("John")
names.Add("Paul")
names.Add("Lea")
names.Add("Jen")
names.Add("Joan")
names.Add("Alex")
names.Add("Jonny")
names.Add("Alexis")
names.Add("Francis")
cmbNames.DataSource = names
End Sub
When user digit on the combobox I would to filter and show only the names that contains the string entered.
So in textchange event I tryed to use .Contains and .findString, but I not get the behavour expected:
if "is" digit, I would have Alexis and Francis as result, if "Jo" is entered it would get John, Jonny and Joan. Instead I still get only the result that start with the string searched.
Any advice?
Re: how to search in a combobox
Why not use the autocomplete functionality built into the ComboBox control?
Re: how to search in a combobox
BTW, you can do this:
vb.net Code:
Dim names As New List(Of String) From {"John", "Paul", ..., "Francis"}
Re: how to search in a combobox
If you mean by:
Code:
AutoCompleteMode=SuggestAppend
AutoCompleteSource=ListItems
it give me ony the names that start with the entered string, not all the name that contain it.
But maybe I misconfigured it.
Re: how to search in a combobox
It should definitely be
Code:
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
for latter property. But neither for none of
Code:
ComboBox1.AutoCompleteMode = AutoCompleteMode.Append 'Autofills, No combo opens
ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest 'No autofills, Combo opens
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend 'Autofills, Combo opens
these, for a list of
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim NamesList As New List(Of String) From {"John Johnson", "Paul McCartney Junior", "Sir Francis Ford Coppola"}
ComboBox1.DataSource = NamesList
End Sub
by pressing only one 'j' you expect that a combo opens with following items but unfortunately you will not get
. John Johnson
. Paul McCartney Junior
(Ignore colors. I tried 4 words names containing 3 'j's in total for a complex example)
as result.
As far as I know ListBox1.FindString will also do the same, so it is also a rejected. What are you looking for can be achieved by a sort of seeking loop that specific entered string in whole string of every item.
Try another way.
Re: how to search in a combobox
Quote:
Originally Posted by
hannibal smith
it give me ony the names that start with the entered string, not all the name that contain it.
You are correct. Sorry, I should have read your question more carefully.
Re: how to search in a combobox
Re: how to search in a combobox
You can try something like this:
Code:
Public Class Form2
Private ReadOnly _names As List(Of String)
Sub New()
InitializeComponent()
_names = New List(Of String) From {
"John",
"Paul",
"Lea",
"Jen",
"Joan",
"Alex",
"Jonny",
"Alexis",
"Francis"
}
cmbNames.Items.AddRange(_names.ToArray())
cmbNames.AutoCompleteMode = AutoCompleteMode.None
End Sub
Private Sub cmbNames_TextChanged(sender As Object, e As EventArgs) Handles cmbNames.TextChanged
Dim caretPosition = cmbNames.SelectionStart
cmbNames.Items.Clear()
Dim input = cmbNames.Text
Dim matches = _names.AsEnumerable()
If (Not String.IsNullOrWhiteSpace(input)) Then
matches = matches.Where(Function(item) item.IndexOf(input, StringComparison.OrdinalIgnoreCase) > -1)
End If
cmbNames.Items.AddRange(matches.ToArray())
cmbNames.SelectionStart = caretPosition
End Sub
End Class
Re: how to search in a combobox
I found out that a third part control with the feature that I need already exist (sfCombobox).
Re: [RESOLVED] how to search in a combobox
Good to hear that but - as an archive record - it might be implemented with like query as we made to lookup in databases, (I don't know how). A also believe in certified 3rd party controls.
Re: [RESOLVED] how to search in a combobox