Solved

VB COMBOBOX:  array with the autocomplete matches?

Posted on 2010-08-26
4
710 Views
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.
0
Comment
Question by:motokent
  • 2
  • 2
4 Comments
 
LVL 53

Expert Comment

by:Dhaest
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) ?
0
 

Author Comment

by:motokent
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.
0
 
LVL 53

Accepted Solution

by:
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

0
 

Author Closing Comment

by:motokent
ID: 33532175
Thank you.  This is good.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

679 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