?
Solved

Capturing delete keystroke in ListView control

Posted on 2005-04-04
8
Medium Priority
?
445 Views
Last Modified: 2008-02-01
Hi everyone,

Can someone tell me how I can capture the delete keystroke in a ListView control. Basically, I want to delete the selected row when delete is pressed by the user.

Thanks and cheers!

xargon
0
Comment
Question by:xargon_123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
8 Comments
 
LVL 10

Assisted Solution

by:fds_fatboy
fds_fatboy earned 1000 total points
ID: 13698500
Private Sub ListView1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDelete Then
        MsgBox "Del key pressed"
    End If

End Sub
0
 
LVL 19

Accepted Solution

by:
Shauli earned 1000 total points
ID: 13698505
Private Sub ListView1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Then
    ListView1.ListItems.Remove ListView1.SelectedItem.Index
End If
End Sub

S
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13698555
Or to do the delete as well...

Private Sub ListView1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDelete Then
        If Not ListView1.SelectedItem Is Nothing Then
            ListView1.ListItems.Remove ListView1.SelectedItem.Key
        End If
    End If
End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13698752
Be careful, the answer you accepted, will cause error 91 to occur if you press delete key on an empty listview.
0
 

Author Comment

by:xargon_123
ID: 13704881
Well, I have another problem.- The VBA environment does not seem to accept the keydown event for the ListView. Says "The procedure declaration does not match description of event or procedure having the same name"
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13705790
Sorry - I didn't realise you were using VBA.

Try this:

Private Sub ListView1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDelete Then
        If Not ListView1.SelectedItem Is Nothing Then
            ListView1.ListItems.Remove ListView1.SelectedItem.Key
        End If
    End If
End Sub
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13705861
Bugger - I copied the wrong thing in..

I meant

Private Sub ListView1_KeyDown(KeyCode As Integer, ByVal Shift As Integer)
...

But I've just tried it (VBA is not my forte) and it doesn't detect Del key.
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 13705955
You could set the KeyPreview asnd capture the keystroke in the form - this appears to work in Access VBA:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDelete And Me.ActiveControl Is ListView1 Then
        If Not ListView1.SelectedItem Is Nothing Then
            ListView1.ListItems.Remove ListView1.SelectedItem.Index
        End If
    End If
End Sub

Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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