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?
debdbaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jake072Commented:
You need to use the OnKeyDown feature.

    Private Sub Combo_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles [ComboBox].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

        ' Store the actual text that has been typed
        Dim TypedText As String = Text

        Dim index As Integer = FindString(TypedText)
        ' Get the text of the first match
        If index > -1 Then

            Dim foundText As String = Me.GetItemText(Items(index))

            Dim sAppendText As String = foundText.Substring(TypedText.Length)
            Text = TypedText & sAppendText

            ' Select the portion of the text that was automatically
            ' added so further typing will replace it
            SelectionStart = TypedText.Length
            SelectionLength = sAppendText.Length
            SelectedIndex = index
        End If

    End Sub

This will essentially create an "AutoComplete" Combobox, which will select the best match to the typed string at all times.

Let me know if you need more help...  Also note you can let the Sub handle as many ComboBox KeyDown's as you want... Personally, I have it in a Custom Derived ComboBox class that I use.

Jake

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
debdbaAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.