Link to home
Start Free TrialLog in
Avatar of debdba
debdba

asked on

Sorted Combobox; As user types it drills down. How to select the first one in the list each time?

Hi,
I have a sorted combobox on my form.  As the user begins to type in the text box, it seems to automatically drill down in the list to the first item that matches what the user has typed so far.  Is there an easy way to select the first item seen in the box each time.

Example:  He's going to type McDonalds...
M - causes the list to shift down to show "MacDermid" first
c - causes the list to shift down to show "McAfee" first
D - causes the list to shift down to show "McData" first
o - causes McDonalds to come up at the top of the list

Is there a way to select the item at the top of the list each time - select "MacDermid", then change the selection to "McAfee", then to "McData", and then to McDonalds.  That way when the user reaches what he's looking for, it will already be selected in the combobox list.

Thanks,
debdba

P.S.  Did I turn something on to cause the drill down to work as it does, or is it just the fact that it is a sorted combobox that does that?
ASKER CERTIFIED SOLUTION
Avatar of jake072
jake072
Flag of Canada 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
Avatar of debdba
debdba

ASKER

Jake,
This is GREAT!!!  Thanks much.  I also like the idea of keeping it for a custom control because it's nice.  I simplified it a bit, although yours is more intuitive/better for understanding it.  
I'm a happy camper!!
debdba

Here's my version:
    Private Sub cboStocks_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboStocks.KeyUp
        ' Do nothing for certain keys such as navigation keys
        With e
            If ((.KeyCode = Keys.Back) Or _
             (.KeyCode = Keys.Enter) Or _
             (.KeyCode = Keys.Left) Or _
             (.KeyCode = Keys.Right) Or _
             (.KeyCode = Keys.Up) Or _
             (.KeyCode = Keys.Delete) Or _
             (.KeyCode = Keys.Down) Or _
             (.KeyCode = Keys.PageUp) Or _
             (.KeyCode = Keys.PageDown) Or _
             (.KeyCode = Keys.Home) Or _
             (.KeyCode = Keys.ShiftKey) Or _
             (.KeyCode = Keys.End) Or _
             (.KeyCode = Keys.Tab AndAlso .KeyCode = Keys.Shift)) Then

                Return
            End If
        End With

        ' Find index in cboStocks of first item that begins with typedtext
        Dim cboindex As Integer = cboStocks.FindString(cboStocks.Text)
        Dim typedlen As Integer = cboStocks.Text.Length

        ' Get the text of the first match and select it in the combobox
        If cboindex > -1 Then
            cboStocks.SelectedIndex = cboindex
            ' Select the portion of the text that was automatically
            ' added so further typing will replace it
            cboStocks.SelectionStart = typedlen
            cboStocks.SelectionLength = 100
        End If
    End Sub