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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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