CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Listview

  1. #1
    Join Date
    Apr 2000
    Location
    Dorado, P.R.
    Posts
    221

    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

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    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.

  3. #3
    Join Date
    Apr 2000
    Location
    Dorado, P.R.
    Posts
    221
    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.

  4. #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
    Web reading made easy -
    http://www.xemantex.com

  5. #5
    Join Date
    Apr 2000
    Location
    Dorado, P.R.
    Posts
    221
    Thanks for your suggestion, y tried it with no results.

    listview1.items(listview1.selectedItem).remove

  6. #6
    Join Date
    Sep 2002
    Location
    Africa
    Posts
    147
    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

  7. #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.
    Web reading made easy -
    http://www.xemantex.com

  8. #8
    Join Date
    Sep 2002
    Location
    Africa
    Posts
    147
    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

  9. #9
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892
    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.

  10. #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!
    Web reading made easy -
    http://www.xemantex.com

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

Featured