Results 1 to 8 of 8

Thread: [2005] Listview Control Problem

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    63

    [2005] Listview Control Problem

    Hey,
    Is there anyway to find out the index of the last selected item in the listview?

    Or on button click extract subitem(2) value from the last selectedrow in the listview. Store it as a variable. Bring a inputbox up, and insert the value entered into the listview subitem(2) field.

    Thanks

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2005] Listview Control Problem


    I don't think listview has any sort of 'lastselecteditem' property. The solution would probably be some sort of hack where you save the selected item to a variable before it get's updated, assuming you can handle an event to catch the selected item before the control changes it. That will be the trick, and I don't know which one it will be.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Listview Control Problem

    when you say last selected, that doesn't really clarify things.

    Do you mean the last items selected when you have multiple items selected? Or do you mean when you have an item selected, then you select a different one, you want to know what the previous one was?

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    63

    Re: [2005] Listview Control Problem

    I was trying to do as dolot suggested and on selecteditem change event save the selected item to a variable. But I didn't know how to get the index of the item.

    Kleinma:
    If I select item A and then click a button that brings an inputbox up on screen (It deselects the item in the listview). so I am trying to find out what the item index (of the last item selected) is so I can edit the item using the value inputted into the inputbox.

    -- No not selecting multiple items.

    Thanks, hope it makes sense.

  5. #5
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2005] Listview Control Problem

    Just to clarify:

    The scenario is that a user selects an item in a listview and then clicks a button, which pulls up a dialog box of some sort to allow them to edit the value of the selected item, correct?

    If that's the case, then all you have to do is save the selecteditem to in the selectedindexchanged event. If you have multiselect = false on your control, then they can only select one item, which will always be item 0 in the selecteditems collection. So you would have something like:
    Code:
    Dim M_SelectedItem As Object
        Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
            M_SelectedItem = ListView2.SelectedItems(0)
        End Sub
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Listview Control Problem

    If you are going to access selecteditems() in the selectedindexchanged event, then you NEED to check the count first, otherwise you will defenitly end up with an nullreferenceexception.

    So you always want to do something like

    Code:
    If ListView2.SelectedItems.Count <> 0 then
        'code here
    end if

  7. #7
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2005] Listview Control Problem

    Nice catch, Kleinma. I'm always bad to forget those little details.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    63

    Re: [2005] Listview Control Problem

    Thanks for the help all.

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