Results 1 to 11 of 11

Thread: Selected Indecies of a list View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    81

    Question Selected Indecies of a list View

    How do I get the selected index of a list view item?

    This is what I tried -

    Index = Convert.ToInt16(lstPortfolio.SelectedIndices)
    Last edited by jrowe; Mar 4th, 2008 at 08:33 PM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Selected Index of a list View

    The SelectedIndices property returns a SelectedIndexCollection, so if you're only allowing single selections then the index of the selection will be in index 0 in the returned collection:
    vb Code:
    1. Index = lstPortfolio.SelectedIndices(0)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    81

    Re: Selected Index of a list View

    How would I get multiple indecies?

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Selected Index of a list View

    You would loop through the collection:
    vb Code:
    1. For Each index As Integer in lstPortfolio.SelectedIndicies
    2.     'use the index variable here
    3. Next
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    81

    Question Re: Selected Index of a list View

    I get the blue squigel of death on Index - variable index hides a variable in an enclosing block

    For Each index As Integer in lstPortfolio.SelectedIndicies
    'use the index variable here
    Next

    Any suggestions?

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Selected Indecies of a list View

    That just means that you're already using a variable called index somewhere else in that block. Just rename it to whatever you want.

    vb Code:
    1. For Each i As Integer in lstPortfolio.SelectedIndicies
    2.     'use the i variable here
    3. Next
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    81

    Re: Selected Indecies of a list View

    I've changed the code to what I have below. I thought posting the whole sub might help


    Code:
    Dim Ind As Integer
            Dim i As Integer
    
            btnGet.Enabled = True
            btnDelete.Enabled = True
            btnUpdate.Enabled = True
            btnAbort.Enabled = True
            For Each Ind As Integer In lstPortfolio.SelectedIndices()     'use the index variable here 
                txtNewTick.Text = Convert.ToString(lstPortfolio.Items(i).SubItems(1))
                txtNewBasis.Text = Convert.ToString(lstPortfolio.Items(i).SubItems(2))
                txtNewShares.Text = Convert.ToString(lstPortfolio.Items(i).SubItems(3))
                txtNewPrice.Text = Convert.ToString(lstPortfolio.Items(i).SubItems(4))
            Next

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    81

    Question Re: Selected Indecies of a list View

    Along these lines,

    How would I get the total number of row selected.

    That is, if I select rows 1, 4, and 7; how do I get RowsSelected to equal 3?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    81

    Question Re: Selected Indecies of a list View

    Also, if the user selects more than one row, how do I delete all those rows selected?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Selected Indecies of a list View

    As Atheist has already said, the SelectedIndices property returns a SelectedIndexCollection. As is the case with ALL collections, if you want to know how many items it contains you get its Count property.

    As for deleting the selected items, you have two main choices:

    1.
    vb.net Code:
    1. For i As Integer = myListView.SelectedIndices.Count - 1 To 0 Step -1
    2.     myListView.Items.RemoveAt(myListView.SelectedIndices(i))
    3. Next i
    2.
    vb.net Code:
    1. For i As Integer = myListView.SelectedItems.Count - 1 To 0 Step -1
    2.     myListView.Items.Remove(myListView.SelectedItems(i))
    3. Next i
    In both cases you should call BeginUpdate and EndUpdate before and after the loop respectively, to prevent the control being redrawn after each removal.

    Note that the loops MUST go backwards to prevent a removal affecting the indexes of the rest of the items to be removed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    81

    Re: Selected Indecies of a list View

    I had seen what Athiest posted and tried it. I had some problems with it and thought it was the syntax he gave me when it was actually a logical error on my part.

    Sorry about that I'm not used to programming in visual basic and I have a tendency to get lost in code like this.

    Thanks to jmcilhinney and athiest for the help.

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