VB COMBOBOX:  array with the autocomplete matches?

Posted on 2010-08-26
Last Modified: 2013-11-26
I am using a combobox with auto-complete enabled.  At runtime, when the text changes I would like to count the number of matches.  If there is only one match, then I'll take some action (without user having to hit "enter", or do anything else).  Is there a collection or array that contains the matches?  It would have the same items that get displayed in the drop-down.
Question by:motokent
  • 2
  • 2
LVL 53

Expert Comment

ID: 33531513
To what is your combobox bound ? From where do the items come ?
Are you sure that you want to count each time a character is entered (on_keypress-event) ?

Author Comment

ID: 33531694
I generate the items in the combobox when the application is started (based on a text file as the data source).

My plan is to check for the number of matches once the (number of characters) > 6.  (Each line has 8 characters.)
Then, if/when there is only one match, I assume that is what the user intends to enter and I process data based on the match.

I can accomplish this by counting the matches in my arraylist that was used to populate the combobox in the beginning, but if I can access the items that autocomplete assigns to the drop-down box, that will save time since autocomplete has already done most of the work for me.
LVL 53

Accepted Solution

Dhaest earned 500 total points
ID: 33532139
You can use the code below. Just call in your keypress-event

Dim searchString As String = "t"

Dim countItems As Integer = ComboBox1.Items.Cast(Of String).ToList().FindAll(New PredicateWrapper(Of String, String)(searchString, AddressOf IsItemValid)).Count

in your keypress

... onkey_press(....)

Dim searchString As String = "t"

Dim countItems As Integer = ComboBox1.Items.Cast(Of String).ToList().FindAll(New PredicateWrapper(Of String, String)(searchString, AddressOf IsItemValid)).Count

end sub

    Function IsItemValid(ByVal item As String, ByVal argument As String) As Boolean

        Return item.StartsWith(argument)

    End Function

    Public Delegate Function PredicateWrapperDelegate(Of T, A)(ByVal item As T, ByVal argument As A) As Boolean

    Public Class PredicateWrapper(Of T, A)

        Private _argument As A

        Private _wrapperDelegate As PredicateWrapperDelegate(Of T, A)

        Public Sub New(ByVal argument As A, ByVal wrapperDelegate As PredicateWrapperDelegate(Of T, A))

            _argument = argument

            _wrapperDelegate = wrapperDelegate

        End Sub

        Private Function InnerPredicate(ByVal item As T) As Boolean

            Return _wrapperDelegate(item, _argument)

        End Function

        Public Shared Widening Operator CType(ByVal wrapper As PredicateWrapper(Of T, A)) As Predicate(Of T)

            Return New Predicate(Of T)(AddressOf wrapper.InnerPredicate)

        End Operator

    End Class

Open in new window


Author Closing Comment

ID: 33532175
Thank you.  This is good.

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now