Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
PeterBaileyUk

asked on

visual studio vb.net key up find method

I am using the following code so that I can find words quickly but I am finding that is making the sub string selections so if I want to find bhp i go B then h then p so not only does bhp enter into the selected items so does the letter B and h.

Can the method be altered so that it finds the words but I make the selection at the end? I didnt write this code I did some searches on google and found it

 Private Sub ListCW_KeyUp(sender As Object, e As KeyEventArgs) Handles ListCW.KeyUp
        Static SearchPattern As String
        SearchPattern &= CStr(Convert.ToChar(e.KeyValue))

        Dim FindIndex As Integer =
            sender.FindString(SearchPattern)

        If FindIndex = -1 Then
            SearchPattern = CStr(Convert.ToChar(e.KeyValue))
            sender.SelectedIndex = sender.FindString(SearchPattern)

        Else
            sender.SelectedIndex = FindIndex

        End If
    End Sub

Open in new window

Avatar of it_saige
it_saige
Flag of United States of America image

By "at the end", I assume you mean after the user finishes typing?  If this is the case, then you want to perform the search when the text entry field loses focus (LostFocus) or is left (Leave).

-saige-
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

yes after they finish typing, so the code doesnt need to change it just goes into a different event?
Correct

Edit:  Actually the code would change since you would no longer be capturing the key as a part of the event.  Instead you would use the text from the sender.  Since sender is an object, you would cast sender to the type that raise the event; e.g. TextBox.

-saige-
I tried leave but it didnt like the argument and i dont think the listbox has a lost focus.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I shall play with that suggestion, would you have used a better type of container more suited to what i am doing? is there one? the data in the listbox is pulled from an sqlserver table but i think you know that already.
Thank you
Glad that I can provide assistance.

To answer your question about a better container.  The closest "out-of-the-box" control that I can think of is a combo-box that utilizes an auto-complete feature.  From a visual perspective, however, your items are really only visible when you have the drop-down opened.

-saige-