RemoveItem in Listbox

  • Thread starter Stephen English
  • Start date
S

Stephen English

Hi
I have populate a series of dates in a list box using the AddItem method and
then I want to give the user the ability to delete one or more of them if the
date is a public holiday. However, it deletes the selected one and then asks
me if I want to delete the next one. My code is below.

Regards
Stephen

Private Sub lstSessionDates_Click()
Dim strMsg As String
Dim intResult As Integer

strMsg = "Are you sure you want to delete a Session Date of " &
Me.lstSessionDates.Value & "?"
intResult = MsgBox(strMsg, vbQuestion + vbYesNo, "Delete?")
If intResult = vbYes Then
Me.lstSessionDates.RemoveItem Me.lstSessionDates.ListIndex
End If

End Sub
 
A

alborg

Hi Stephen:

Your code is fixed on single selection mode... gotta change that!

What I would do is to use a Do While... Loop event which would loop through
all of your listbox items and delete those dates selected:

1) Create a clickbutton, and for this example, call it
"btnDeleteSessionDates".
2) Get rid of the code behind your listbox- you want your end user to be
able to select multiple dates without a popup, no?
3) Place your code as a clickbutton (btnDeleteSessionDates) event!

Private Sub btnDeleteSessionDates_Click()
Dim strMsg As String, rr 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
rr = Me.lstSessionDates.ListCount
Do While rr > 0
Me.lstSessionDates.RemoveItem (Me.lstSessionDates.ListIndex)
rr = rr - 1
Loop
End If
End Sub

That should work- try it out and let us know!

Cheers,
Al
 
S

Stephen English

Hi Al
Thank you so much for your speedy reply
Yes, that works except that you have to test if selected!
The reason I was putting it on the click is that there is unlikely to be
more than one
public holiday during a course. However it obviously didn't like the click
event and your suggestion of a button is much better.
Thanks again
Stephen

Private Sub cmdDelete_Click()
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 = Me.lstSessionDates.ListCount
Do While intR > 0
If Me.lstSessionDates.Selected(intR) Then
Me.lstSessionDates.RemoveItem (Me.lstSessionDates.ListIndex)
End If
intR = intR - 1
Loop
End If

End Sub
 
A

alborg

Hi Stephen:

What was I thinking!? My kids were jabbing me in the sides, bothering me to
no end... I actually had a routine to check for selection, then at the last
moment took it out.

This is what I initially had, which is an alternative way of doing it-

If Me.lstSessionDates.ListIndex = -1 Then 'i.e. listindex = -1 is "not
selected"
Me.lstSessionDates.RemoveItem (Me.lstSessionDates.ListIndex)
End If

Anyhow, I'm glad it works now!

Cheers,
Al
 
G

Gordon Bentley-Mix

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.
 
S

Stephen English

Hi Gordon
Thank you for all your great ideas. Your interest is greatrly appreciated
Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top