Listview Ctrl Delete Problem

I am using the Listview ctrl in the report view and am able to delete an entry that has been selected with no problem. If I call the delete routine again without selecting an entry, the next or previous entry is deleted. How do I detect that nothing is currently selected in the Listview ctrl; i.e., I want the user to explicitly select a Listview entry for deletion. Below is the code that is currently doing the job.

strKey = lvwToppings.SelectedItem.Text
  n = MsgBox("Delete this topping?", vbYesNo, "Delete entry")
  If (n = vbYes) Then
    strRemoveKey = "L" & strKey
    lvwToppings.ListItems.Remove strRemoveKey
    n = lvwToppings.ListItems.Count
    If (n = 0 Or n = 1) Then
      curToppingCost = 0
    Else
      curToppingCost = (n - 1) * arrToppings(mintRow)
    End If
    lblToppingPrice.Caption = Format(curToppingCost, "currency")
  End If
tmbtmbAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
manojaminConnect With a Mentor Commented:
Glad that helped you... I am just entering this comment as an answer so that you can award points...

(BTW: you can alos treat the comment as an answer. Most of the time people will put comment rather than answer so that question does not get locked and people don't think that "oh! it's been answered, why bother?")
0
 
tmbtmbAuthor Commented:
This code is part of a project that my second semester vb students will modify next term.

I feel that before an entry is to be deleted, my code should be able to verify (as in checking a listbox's ListIndex property = -1) to determine that an entry has been explcitly selected (or not selected).
0
 
manojaminCommented:
once you delete the entry,
call

lvwToppings.Refresh
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
manojaminCommented:
'do this

Private Sub DeleteThis()
    If (lvwToppings.SelectedItem.Selected) Then
        'strKey = lvwToppings.SelectedItem.Text
        n = MsgBox("Delete this topping?", vbYesNo, "Delete entry")
        If (n = vbYes) Then
            'strRemoveKey = "L" & strKey
            Call lvwToppings.ListItems.Remove(lvwToppings.SelectedItem.Index)
            'lvwToppings.ListItems.Remove strRemoveKey
            lvwToppings.Refresh
            n = lvwToppings.ListItems.Count
            If (n = 0 Or n = 1) Then
                curToppingCost = 0
            Else
                curToppingCost = (n - 1) * arrToppings(mintRow)
            End If
            lblToppingPrice.Caption = Format(curToppingCost, "currency")
        End If
    End If
End Sub
Private Sub lvwToppings_KeyDown(KeyCode As Integer, Shift As Integer)
    If (KeyCode = vbKeyDelete) Then Call DeleteThis
End Sub

0
 
tmbtmbAuthor Commented:
Thanks for your help. That was just what I needed.
0
 
tmbtmbAuthor Commented:
okay, so the questions got answered. how do you award the points?
0
All Courses

From novice to tech pro — start learning today.