I searched through nearly every article in the forum as well as other groups but I'm stumped on a solution. I have a ComboBox filled with values from a DB. I would like autocomplete to show results from the entire string not just the beginning of the string. Autocomplete queries from the beginning only.

Any Help would be apreciated. I've provided an example below for clarity.

If I type the letter "c", everything with a "c" should show in the dropdown because it contains that letter. As I continue to type the choices are then narrowed down.

Wireless Number Change
Address/Email Address/Contact Numbers
Mobile Charges
Equipment Charges
Data Charges

Early termination fee
Equipment Fee
International Charges
Late Payment Fee
Purchase Charges
Reinstate/Restoral Fee

Recommended Answers

All 6 Replies

Which UI are you using?

That is a language, not a UI

I'm using Visual Studio

As far as I know, auto-complete only matches from the beginning of the string. If you want to match any string at any position you'll have to roll your own. You could populate a listbox with every keystroke to contain only the matching strings. The user could click (or double-click) to select from the listbox.

I grabbed this code from an another post. This is similar to what I'm looking for but it only lloks for text after the ":" If I remove the substrings reference the ":", it just does a regular AutoComplete.

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With ComboBox1
            '//-- set for AutoComplete.
            .AutoCompleteSource = AutoCompleteSource.CustomSource
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend '--\\
            AddHandler .Validating, AddressOf cmb_Validating '// add ComboBox to Validating Event.
        End With
        '// use this to load/reload the AutoCompleteList with the ComboBox items.
        loadMyCoolAutoCompleteList(ComboBox1)
    End Sub
    Private Sub loadMyCoolAutoCompleteList(ByVal selectedComboBox As ComboBox)
        With selectedComboBox
            .AutoCompleteCustomSource.Clear() '// Clear AutoCompleteList.
            For Each itm As String In selectedComboBox.Items '// loop thru all items in the ComboBox.
                .AutoCompleteCustomSource.Add(itm) '// add original item to your AutoCompleteList.
                '// locate the .Substring you want to add to the AutoCompleteList.
                itm = itm.Substring(itm.IndexOf(":") + 2) '// get all text following the ":" and the " " right after it.
                .AutoCompleteCustomSource.Add(itm) '// add .Substring of item to your AutoCompleteList.
            Next
        End With
    End Sub
    '// once the ComboBox Validates (looses focus), it will set the original item as .Text.
    Private Sub cmb_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim selectedComboBox As ComboBox = CType(sender, ComboBox)
        For Each itm As String In selectedComboBox.Items '// loop thru all items in the ComboBox.
            If itm.Substring(itm.IndexOf(":") + 2) = selectedComboBox.Text Then '// locate the .Substring that matches the .Text.
                selectedComboBox.Text = itm '// set .Text of ComboBox item.
                Exit For '// exit loop since done locating item.
            End If
        Next
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.