-
Listview
Hi everybody;
I'am currently changing from VB6 to VB.Net 2003. I'am searching msn.microsoft.com for how to obtain the index of the selected item in the listview. Can anyone help me with this. I need to obtain this number, so i can delete the record from the listview.
Any help please
-
Listview.SelectedItems will return a collection of all the selected items, if only one is select, then use ListView.SelectedItems(0).
Alternatively, Listview.SelectedIndices will return a collection of all the selected indexes.
-
Thanks for your time & support.
I did it but it not work, maybe I'am doing something bad.
in VB6 i will do it like this
dim A as integer
A = ListView1.SelectedItem.index
ListView1.ListItems.Remove (A)
How can i make this in VB.Net
Please if you can give me a short example like the one i post in vb6 it will be strongely apreciated.
-
In order to delete the selected item from the list view you can use the following single line of code:
Code:
lstView.items(lstView.selectedItem).remove
If you want to remove any other item from lstView for which the index is known then use the following code:
Code:
lstView.items(idx).remove
-
Thanks for your suggestion, y tried it with no results.
listview1.items(listview1.selectedItem).remove
-
Had that same problem and this is what I did
I used the . . . SelectedIndexChanged event
and the code was like . . . A = ListView1.SelectedItems(0).Index
and that's how i got the Index.
For some funny reason it is not working in the click or eventclick as I expected. Could someone tell me why :(
-
adComp,
I am sorry that I was a bit lethargic in my previous reply.
There is no property for a listview called selecteditem.
As DSJ said listView.SelectedItems will return a collection object of type listView.SelectedListViewItemCollection. You can iterate thru this collection object and delete one (or all) the selected items. I have some code for you below:
Code:
Dim selCol As ListView.SelectedListViewItemCollection 'declare a collection object
Dim selColItem As ListViewItem 'declare a listview item
selCol = Me.ListView1.SelectedItems 'get all the items selected
Dim idx As Integer 'index
'iterate thru selected items and delete one after one
'if there is only one selection then ONLY that item
'will be deleted
For Each selColItem In selCol
Try
idx = selColItem.Index 'get index (but not needed)
selColItem.Remove() 'delete the selectedItem
Catch ex As Exception
End Try
Next
hey Platinum Plus I think you shud try this one. Hope it will solve your problems.
-
Thanks gasperCasper
It works fine in the SelectedIndexChanged event
Is that the new way of doing it or can I still run it in the click event???
-
I would avoid SelectedIndexChanged all together, as it has quite a few bugs that aren't being fixed until the next .NET release. Stick with the Click event.
-
It shud not matter if you use the code in _click event or _selectedIndexChanged event. In either case, you would iterate thru the "selectedItems" to perfrom desired operations (delete, etc).
as far as bugs in _selectedIndexChanged concerned, I am not sure. But, I know that this event fires on form_load, form_minimize, form_hide and so many similar events.. it is tricky to handle this event!