-
Jun 6th, 2023, 05:59 AM
#1
Thread Starter
Addicted Member
[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?
-
Jun 6th, 2023, 06:47 AM
#2
Re: how to search in a combobox
Why not use the autocomplete functionality built into the ComboBox control?
-
Jun 6th, 2023, 06:49 AM
#3
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"}
-
Jun 6th, 2023, 07:08 AM
#4
Thread Starter
Addicted Member
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.
-
Jun 6th, 2023, 11:53 AM
#5
Hyperactive Member
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.
Last edited by pourkascheff; Jun 6th, 2023 at 03:32 PM.
Reason: Color grading for better understanding.
-
Jun 6th, 2023, 08:25 PM
#6
Re: how to search in a combobox
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.
-
Jun 7th, 2023, 12:46 AM
#7
Re: how to search in a combobox
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jun 8th, 2023, 11:00 AM
#8
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
-
Jun 8th, 2023, 03:40 PM
#9
Thread Starter
Addicted Member
Re: how to search in a combobox
I found out that a third part control with the feature that I need already exist (sfCombobox).
-
Jun 11th, 2023, 01:07 PM
#10
Hyperactive Member
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.
-
Jun 12th, 2023, 02:05 PM
#11
Thread Starter
Addicted Member
Re: [RESOLVED] how to search in a combobox
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|