Results 1 to 8 of 8

Thread: [RESOLVED] Delete the last item in listview

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Resolved [RESOLVED] Delete the last item in listview

    Hello VbForums
    I want to delete items from a listview and database in Listview ItemCheck event.
    But I want to start deleting from the last item.
    I want to prevent the user from deleting an item from the beginning or the middle of the listview list.
    If he does so, he gets a message that he should check the last item in the list.
    this is my code for deleting:
    Code:
    cnn.Execute "delete from tbl1  where ID =  _
            "  " & ListView1.ListItems.Item(i).SubItems(1) & ""
    ListView1.ListItems.Remove (i)
    If this is not possible, I wish to start deleting records from the most recent date in database and prevent users deleting a record if it is not the most recent date.
    I use SQliteand VbrichClient.
    thank you in advance.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Delete the last item in listview

    ?
    Just check the Index of SelectedItem against ListItems.Count
    Beware, if your ListView allows MultiRow-Selection

    btw: Is your ListView sortable? If it's sortable you could run into the fact, that in "first" run your order is as you want it, and the user cannot delete a row from the middle.
    He clicks a Header to sort, and that row is suddenly the last one, and he can delete
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: Delete the last item in listview

    Just check the Index of SelectedItem against ListItems.Count
    thank you sir for your interest
    this is what I did but it did not work
    Code:
    Dim idx As Integer
    
        For idx = 1 To lvw.ListItems.Count
            If lvw.ListItems(idx).Checked < lvw.ListItems.Count Then
    MsgBox "You must start deleting from the bottom of the list", vbCritical
    
            End If
        Next
    thanks

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Delete the last item in listview

    Of course it didn't work... because that's not right... and if you debugged it, you'd see why.

    What do you think this does:
    Code:
    lvw.ListItems(idx).Checked
    Because I know what it does. It returns a value based on whether the value is checked (1), or not (0), or indeterminate (-1) ... so assumming your item is not checked, it returns 0... 0 will be less than your count of items... so your If statement will always return true...

    That's not what you want. At all.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: Delete the last item in listview

    techgnome thank you for your interest
    would you please help me with the right code?

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Delete the last item in listview

    Well the first thing that jumps out at me is that if you want to delete the last item then why do you loop though all the items from the beginning? Would it not make a lot more sense to start at the last item and go the other way?

    Code:
    For idx = lvw.ListItems.Count to 1 step -1
    Of course a loop only makes sense if you are wanting to delete more than one item. If you only want to delete the last item then just check the last item and don't use a loop at all.
    Code:
    If  lvw.ListItems( lvw.ListItems.Count).Checked  Then
       'go ahead and delete the item
    Else
       MsgBox "You must start deleting from the bottom of the list", vbCritical
    End If
    Also wondering why it would matter if they deleted from the bottom or not, one would think that the user should be able to delete which ever item they wish without being forced to delete all the items that occur after it and especially not require them to check the items from the bottom when they actually want to just delete one in the middle? Is there a reason for this?

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Delete the last item in listview

    The code below would handle deleting any and all checked items in the listview no matter the number or location of said items
    Code:
    Dim DeletedItems as integer
    For idx = lvw.ListItems.Count to 1 step -1
        if lvw.ListItems(idx).Checked then
            'add code to delete this item
            DeletedItems=DeletedItems+1
        End If
    Next
    Msgbox Cstr(DeletedItems) & " Items Deleted"

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2013
    Posts
    783

    Re: Delete the last item in listview

    DataMiser
    thank you very much for the precious help
    solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width