Stephen,
If you 'modularise' your code to make the actual Item deletion process a
separate procedure, then you can support the use of the "Delete" key from the
keyboard as well. Something like the following should work:
Private Sub lstSessionDates_Keydown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift as Integer)
If KeyCode = 46 Then
If lstSessionDates.ListIndex > 0 Then
DeleteSessionListItem
End If
End If
End Sub
Private Sub cmdDelete_Click()
If lstSessionDates.ListIndex > 0 Then
DeleteSessionListItem
End If
End Sub
Private Sub DeleteSessionListItem
Dim strMsg As String
Dim intR As Integer
Dim intResult As Integer
strMsg = "Are you sure you want to delete the selected session date(s)?"
intResult = MsgBox(strMsg, vbQuestion + vbYesNo, "Delete?")
If intResult = vbYes Then
intR = lstSessionDates.ListCount
Do While intR > 0
If lstSessionDates.Selected(intR) Then
lstSessionDates.RemoveItem (lstSessionDates.ListIndex)
End If
intR = intR - 1
Loop
End If
End Sub
The above could probably be a bit cleaner but I "borrowed" it from a live
project of mine that works a bit differently to yours - but I'm sure you get
the idea.
BTW, I'm not sure how you are populating your ListBox initially (other than,
as you said, using .AddItem), but if you were to use an array as the
"container" for your items rather than the ListBox itself, you could approach
this a bit differently. The project I took this example from is designed to
allow the user to add, modify or delete "sets" of similar information -
Customer details in a sales agreement. These data sets are stored in a
two-dimensional array, and the array is used to populate the ListBox. I won't
go into the full detail of how the data is added to or maintained within the
array or how the ListBox is populated, but the code for deleting data sets is
as follows:
Private Sub lstCustomers_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
If KeyCode = 46 Then
DeleteCustomer
End If
End Sub
Private Sub btnDeleteCustomer_Click()
DeleteCustomer
End Sub
Private Sub DeleteCustomer()
If lstCustomers.ListIndex > 0 Then
Dim DelIndex As Long
Dim myResult As Long
DelIndex = lstCustomers.ListIndex + 1
myResult = MsgBox("Delete Customer " & CustomersArray(0, DelIndex),
vbYesNo, "Delete Customer")
If myResult = vbYes Then
DeleteCustomerListItem DelIndex
PopulateCustomersList
End If
End If
End Sub
Private Sub DeleteCustomerListItem(DeleteLine As Long)
Dim i As Long
Dim n As Long
CustomerCount = CustomerCount - 1
If DeleteLine <= CustomerCount Then
For i = DeleteLine To CustomerCount
For n = 0 To 1
CustomersArray(n, i) = CustomersArray(n, i + 1)
Next n
Next i
End If
If CustomerCount > 0 Then
ReDim Preserve CustomersArray(1, 1 To CustomerCount) As Variant
Else: Erase CustomersArray
End If
End Sub
Private Sub PopulateCustomersList()
Dim n As Long
With lstCustomers
.Clear
If CustomerCount > 0 Then
For n = 1 To CustomerCount
.AddItem CustomersArray(0, n)
.List(n - 1, 1) = CustomersArray(1, n)
Next n
End If
End With
End Sub
Might not be applicable to your current project but could be useful for you
in the future...
--
Cheers!
Gordon Bentley-Mix
Word MVP
Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.