How do I get a combobox to scroll using arrow keys?

Posted on 2014-07-16
Last Modified: 2014-07-16
I have written a Windows Form Application where I have a form that has a ComboBox control. When using the Mouse I can scroll through the list and select the record I require with no problem. Unfortunately my user wants to be able to use the up / down arrow keys. This doesn't soon as I touch either key the record is selected and control returns to the form.

I have tried adding code to the KeyDown / KeyUp to replicate what I think the scrolling mouse would do but this doesn't work either!

I have copied the code below for reference.

Private Sub cmbCustomerName_KeyDown(sender As Object, e As KeyEventArgs) Handles cmbCustomerName.KeyDown

        Dim items As Integer = cmbCustomerName.Items.Count
        Dim i As Integer

        i = cmbCustomerName.SelectedIndex

        Select Case e.KeyCode
            Case Keys.Down
                cmbCustomerName.Text = cmbCustomerName.Items.Item(i + 1).ToString
            Case Keys.Up
                If i <> 0 Then
                    cmbCustomerName.Text = cmbCustomerName.Items.Item(i - 1).ToString
                End If
            Case Keys.Left

            Case Keys.Right

            Case Keys.Enter
        End Select

    End Sub
Question by:abmcsltd
    LVL 12

    Expert Comment

    Works fine for me.  Although I'm using VS2008.  When I populate a combobox with data I can scroll thru it with the up / down arrow keys, without having to code anything.
    LVL 83

    Expert Comment

    >as soon as I touch either key the record is selected and control returns to the form.

    What code do you have in which event which selects the record and closes the form? I am guessing you have another key press event which needs to ignore the up/down keys.

    Author Comment

    Hi CodeCruiser

    The only other events that relate to cmbCustomerName that are used are as follows:


    Are you saying that either / both of these need to be changed?


    LVL 83

    Accepted Solution

    As you said that as soon as you press a key, record is selected and form is closed so there must be an event at form level which is doing that.

    Author Comment

    Hi CodeCruiser

    You were spot on, the code within the cmbCustomerName_TextChanged was causing the issue! I moved this in its entirity to within the Keys.Enter case of cmbCustomerName_KeyDown and it worked first time!

    Many thanks


    Author Closing Comment

    Quick response with the answer I needed

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now