How do I get the selected index of a list view item?
This is what I tried -
Index = Convert.ToInt16(lstPortfolio.SelectedIndices)
Printable View
How do I get the selected index of a list view item?
This is what I tried -
Index = Convert.ToInt16(lstPortfolio.SelectedIndices)
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:
Index = lstPortfolio.SelectedIndices(0)
How would I get multiple indecies?
You would loop through the collection:
vb Code:
For Each index As Integer in lstPortfolio.SelectedIndicies 'use the index variable here Next
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?
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:
For Each i As Integer in lstPortfolio.SelectedIndicies 'use the i variable here Next
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
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?
Also, if the user selects more than one row, how do I delete all those rows selected?
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.2.vb.net Code:
For i As Integer = myListView.SelectedIndices.Count - 1 To 0 Step -1 myListView.Items.RemoveAt(myListView.SelectedIndices(i)) Next iIn both cases you should call BeginUpdate and EndUpdate before and after the loop respectively, to prevent the control being redrawn after each removal.vb.net Code:
For i As Integer = myListView.SelectedItems.Count - 1 To 0 Step -1 myListView.Items.Remove(myListView.SelectedItems(i)) Next i
Note that the loops MUST go backwards to prevent a removal affecting the indexes of the rest of the items to be removed.
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.