|
-
June 10th, 2004, 01:37 PM
#1
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
-
June 10th, 2004, 04:18 PM
#2
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.
-
June 10th, 2004, 05:32 PM
#3
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.
-
June 10th, 2004, 10:30 PM
#4
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
-
June 10th, 2004, 11:05 PM
#5
Thanks for your suggestion, y tried it with no results.
listview1.items(listview1.selectedItem).remove
-
June 11th, 2004, 07:49 AM
#6
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
Jesus died for my sin and now I am free
-
June 11th, 2004, 09:56 PM
#7
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.
Last edited by gasperCasper; June 11th, 2004 at 09:58 PM.
-
June 15th, 2004, 08:39 AM
#8
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???
Jesus died for my sin and now I am free
-
June 15th, 2004, 03:45 PM
#9
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.
Good Luck,
Craig - CRG IT Solutions - Microsoft Gold Partner
-My posts after 08/2015 = .NET 4.x and Visual Studio 2015
-My posts after 11/2011 = .NET 4.x and Visual Studio 2012
-My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
-My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
-My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
-My posts before 04/2007 = .NET 1.1/2.0
*I do not follow all threads, so if you have a secondary question, message me.
-
June 15th, 2004, 09:55 PM
#10
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!
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
|