[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2014-07-16
Medium Priority
Last Modified: 2014-07-16
I have written a vb.net 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 work....as 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
  • 3
  • 2
LVL 13

Expert Comment

ID: 40199324
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

ID: 40199415
>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

ID: 40199499
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?


Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 83

Accepted Solution

CodeCruiser earned 2000 total points
ID: 40199593
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

ID: 40199756
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

ID: 40199764
Quick response with the answer I needed

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

831 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