-
Sep 22nd, 2020, 08:07 PM
#1
Thread Starter
Fanatic Member
[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.
-
Sep 23rd, 2020, 01:20 AM
#2
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
-
Sep 23rd, 2020, 05:16 AM
#3
Thread Starter
Fanatic Member
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
-
Sep 23rd, 2020, 08:32 AM
#4
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
-
Sep 23rd, 2020, 09:03 AM
#5
Thread Starter
Fanatic Member
Re: Delete the last item in listview
techgnome thank you for your interest
would you please help me with the right code?
-
Sep 23rd, 2020, 12:43 PM
#6
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?
-
Sep 23rd, 2020, 12:53 PM
#7
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"
-
Sep 23rd, 2020, 04:08 PM
#8
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|