Hi,

I have the following code which works pretty fine with Listview. But I would like to use it with computer keyboard up/down arrow keys. How to modify the following code? Please do help.

Private Sub LVW_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LVW.Click

        txtTAID.Text = LVW.SelectedItems(0).Text
        txtVNO.Text = LVW.SelectedItems(0).SubItems(1).Text
        txtVNM.Text = LVW.SelectedItems(0).SubItems(2).Text
    End Sub

Dani AI

Generated

Good job getting this working, — and thanks to for the KeyDown steer. A quick clarification that explains why you saw errors: arrow keys are non-character keys so they do not raise KeyPress reliably; handle KeyDown/KeyUp and test the key with the Keys enum (for example check e.KeyCode = Keys.Up or Keys.Down) instead of using e.KeyChar numeric values.

A cleaner pattern is to centralize the UI update into one routine and call it from the event that truly represents a selection change. For example, handle the ListView.SelectedIndexChanged event (it fires for mouse clicks and keyboard navigation), and in that handler do a safe check like If LVW.SelectedItems.Count = 0 Then Exit Sub before reading the chosen item and updating the textboxes. That removes duplicate code in Click/KeyDown/KeyUp and avoids the common "index out of range" exceptions.

Quick troubleshooting tips that helped others in similar setups:

  • If arrow keys seem ignored, set the form’s KeyPreview = True or handle the ListView.PreviewKeyDown so navigation keys are seen by your code.
  • Use Keys.Up / Keys.Down in KeyDown rather than numeric char codes.
  • Ensure FullRowSelect and MultiSelect are configured the way you expect.
  • When you change selection in code, call EnsureVisible so the user can see the row.

If you want, post your current event handlers and I will show the minimal SelectedIndexChanged helper to cleanly wire everything together.

Recommended Answers

All 8 Replies

you need to handle the keydown event of the control.

you need to handle the keydown event of the control.

Private Sub LVW_KEYDOWN()
        txtTAID.Text = LVW.SelectedItems(0).Text
        txtVNO.Text = LVW.SelectedItems(0).SubItems(1).Text
        txtVNM.Text = LVW.SelectedItems(0).SubItems(2).Text
   End Sub

It also doesn't work. Any help with example, please.

Regards.

You should go to the events of the listview and handle Keypress event. Then in the event, you should use the case method like this:

Select Case e.KeyChar
case 37: // <--- left arrow.
        // do stuff for Left Arrow here.
    case 38: // <--- up arrow.
// do stuff for Up Arrow here.
case 39: // <--- right arrow.
// do stuff for Right Arrow here.
case 40: // <--- down arrow.
// do stuff for Down Arrow here.
End Select

Hope i helped!

You should go to the events of the listview and handle Keypress event. Then in the event, you should use the case method like this:

Select Case e.KeyChar
case 37: // <--- left arrow.
        // do stuff for Left Arrow here.
    case 38: // <--- up arrow.
// do stuff for Up Arrow here.
case 39: // <--- right arrow.
// do stuff for Right Arrow here.
case 40: // <--- down arrow.
// do stuff for Down Arrow here.
End Select

Hope i helped!

I am getting an error: Name 'e' is not declared. How can I declare it? Please help.

Private Sub LVW_KEYPRESS()
        Select Case e.KeyChar
            Case 37 '<--- left arrow.
                txtTAID.Text = LVW.SelectedItems(0).Text
                txtVNO.Text = LVW.SelectedItems(0).SubItems(1).Text
                txtVNM.Text = LVW.SelectedItems(0).SubItems(2).Text
            Case 38 '<--- up arrow.
                txtTAID.Text = LVW.SelectedItems(0).Text
                txtVNO.Text = LVW.SelectedItems(0).SubItems(1).Text
                txtVNM.Text = LVW.SelectedItems(0).SubItems(2).Text
            Case 39 '<--- right arrow.
                txtTAID.Text = LVW.SelectedItems(0).Text
                txtVNO.Text = LVW.SelectedItems(0).SubItems(1).Text
                txtVNM.Text = LVW.SelectedItems(0).SubItems(2).Text
            Case 40 '<--- down arrow.
                txtTAID.Text = LVW.SelectedItems(0).Text
                txtVNO.Text = LVW.SelectedItems(0).SubItems(1).Text
                txtVNM.Text = LVW.SelectedItems(0).SubItems(2).Text
        End Select
    End Sub

The method you are using is completely wrong mate... :S


When on visual studio do the following:
1)Click the listview
2)Then go on the properties panel
3)on top, there is a lighting bolt...Click it
4)Search for 'Keypress' Event
5)Double click the textbox in the right
6)Post the code that i provided.

Cheers :D

The method you are using is completely wrong mate... :S


When on visual studio do the following:
1)Click the listview
2)Then go on the properties panel
3)on top, there is a lighting bolt...Click it
4)Search for 'Keypress' Event
5)Double click the textbox in the right
6)Post the code that i provided.

Cheers :D

Thanks a lot for your kind help. I am sorry, actually I was looking for the keyup & keydown event but it was my mistake to mention as keypress event.

The following code works fine for keyup & key down events:

Private Sub LVW_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LVW.KeyDown
        txtTAID.Text = LVW.SelectedItems(0).Text
        txtVNO.Text = LVW.SelectedItems(0).SubItems(1).Text
        txtVNM.Text = LVW.SelectedItems(0).SubItems(2).Text
    End Sub
Private Sub LVW_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LVW.KeyUp
        txtTAID.Text = LVW.SelectedItems(0).Text
        txtVNO.Text = LVW.SelectedItems(0).SubItems(1).Text
        txtVNM.Text = LVW.SelectedItems(0).SubItems(2).Text
    End Sub

I am eagerly waiting for search button code for which there is a post. Please help me out.

Once again thanks for your kind reply.

No problem mate :)

I am eagerly waiting for search button code for which there is a post. Please help me out.

Could you be more spesific? What post?

No problem mate :)

Could you be more spesific? What post?

Thanks for the reply. It is solved.

Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.