|
-
Mar 4th, 2008, 08:10 PM
#1
Thread Starter
Lively Member
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.
-
Mar 4th, 2008, 08:14 PM
#2
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:
Index = lstPortfolio.SelectedIndices(0)
-
Mar 4th, 2008, 08:21 PM
#3
Thread Starter
Lively Member
Re: Selected Index of a list View
How would I get multiple indecies?
-
Mar 4th, 2008, 08:23 PM
#4
Re: Selected Index of a list View
You would loop through the collection:
vb Code:
For Each index As Integer in lstPortfolio.SelectedIndicies
'use the index variable here
Next
-
Mar 4th, 2008, 08:33 PM
#5
Thread Starter
Lively Member
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?
-
Mar 5th, 2008, 06:14 AM
#6
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:
For Each i As Integer in lstPortfolio.SelectedIndicies
'use the i variable here
Next
-
Mar 6th, 2008, 06:39 PM
#7
Thread Starter
Lively Member
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
-
Mar 20th, 2008, 07:13 PM
#8
Thread Starter
Lively Member
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?
-
Mar 22nd, 2008, 10:40 AM
#9
Thread Starter
Lively Member
Re: Selected Indecies of a list View
Also, if the user selects more than one row, how do I delete all those rows selected?
-
Mar 22nd, 2008, 10:53 AM
#10
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:
For i As Integer = myListView.SelectedIndices.Count - 1 To 0 Step -1 myListView.Items.RemoveAt(myListView.SelectedIndices(i)) Next i
2.
vb.net Code:
For i As Integer = myListView.SelectedItems.Count - 1 To 0 Step -1 myListView.Items.Remove(myListView.SelectedItems(i)) 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.
-
Mar 22nd, 2008, 11:20 AM
#11
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|