Answered by:
Visual Basic - How to determined the index of a item in ListView

Question
-
I have determined the item in a listview that I with to remove (by searching by way of my code). Now I would like to determined the index of that item so I can use removeat(index) to remove the item. How do I determined the index? or can I remove that item without knowing the index? Please help.
Ed.
ecb
Monday, December 31, 2012 12:09 AM
Answers
-
Public Class Form1 Dim LViewToDelete As Integer = 10000000 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ListView1.AllowColumnReorder = True ListView1.GridLines = True ListView1.View = View.Details ListView1.Sorting = SortOrder.Ascending ListView1.Columns.Add("Instance", 200, HorizontalAlignment.Left) ListView1.Columns.Add("Instance Nr 1", 200, HorizontalAlignment.Left) ListView1.Columns.Add("Instance Nr 2", 200, HorizontalAlignment.Left) ListView1.Columns.Add("Instance Nr 3", 200, HorizontalAlignment.Left) Dim item1 As New ListViewItem("1st instance") item1.SubItems.Add("1st instance 1") item1.SubItems.Add("1st instance 2") item1.SubItems.Add("1st instance 3") ListView1.Items.AddRange(New ListViewItem() {item1}) Dim item2 As New ListViewItem("2nd instance") item2.SubItems.Add("2nd instance 1") item2.SubItems.Add("2nd instance 2") item2.SubItems.Add("2nd instance 3") ListView1.Items.AddRange(New ListViewItem() {item2}) Timer1.Interval = 2000 Timer1.Start() End Sub Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) ListView1.FindItemWithText(TextBox1.Text).Index.ToString() End Sub Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged End Sub Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then Try LViewToDelete = CInt(ListView1.FindItemWithText(TextBox1.Text).Index.ToString()) Catch ex As Exception End Try If LViewToDelete <> 10000000 Then Try ListView1.Items.RemoveAt(LViewToDelete) Catch ey As Exception End Try End If End If LViewToDelete = 10000000 End Sub End Class
You've taught me everything I know but not everything you know.
- Marked as answer by Mark Liu-lxf Monday, January 7, 2013 2:54 AM
Monday, December 31, 2012 6:14 AM -
Ed,
That is probably no true code, from1?????????????????????????????????
However, I've written this in this forum endless times, people want for some reason use a listview like a DataGridView, that goes wrong as soon as you start doing the things you want to do.
Success
Cor- Proposed as answer by Mr. Monkeyboy Monday, December 31, 2012 9:07 AM
- Marked as answer by Mark Liu-lxf Monday, January 7, 2013 2:54 AM
Monday, December 31, 2012 9:01 AM -
you can try just few lines:
Dim ListItem As ListViewItem = listView1.FindItemWithText("test") Dim index As Integer = ListItem.Index listView1.Items.RemoveAt(index) ' ------------OR------------ listView1.Items.Remove(ListItem)
thanks
Hirendra Sisodiya from authorcode.com
- Proposed as answer by JohnWein Monday, December 31, 2012 10:34 AM
- Marked as answer by Mark Liu-lxf Monday, January 7, 2013 2:54 AM
Monday, December 31, 2012 9:09 AM
All replies
-
Ed,
Have a look at something I posted yesterday morning here:
It's the last reply if that doesn't open correctly. In it, you'll see that I have a ListView and the user has clicked the "Remove" button and I remove that item based not on the index, but on the item they've chosen (i.e., the one in the collection of the SelectedItems).
Please call me Frank :)
Monday, December 31, 2012 12:19 AM -
Let me try that again - that's why I shouldn't "edit code on the fly" here:
Private Sub btn_RemoveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btn_RemoveFile.Click ' If ListView1.SelectedItems.Count > 0 Then ListView1.BeginUpdate() ' For i As Integer = ListView1.Items.Count - 1 To 0 Step -1 If ListView1.Items(i).Selected Then Dim lvi As ListViewItem = ListView1.Items(i) ListView1.Items.Remove(lvi) End If Next ' ListView1.EndUpdate() End If ' End Sub
Do you see why I have to go through the list in reverse order?Please call me Frank :)
Monday, December 31, 2012 12:41 AM -
I won't be online tonight much longer, so let me answer my own question:
When you tell it to start at the top and look to the bottom (i.e., For i As Integer = 0 To ListView1.Items.Count - 1), then you can't change anything that will alter that bottom-most index number. If you think about it, that just makes sense. Let's say I had ten items (index 0 through 9) and I remove one. Now there IS not item at index 9, right?
That's why it will throw an exception.
On the other hand, if you tell it to start at the bottom and look to the top, it doesn't look behind it - it's looking to the top (index 0) which you didn't change. That's why that second method works.
Another way would be to create a temporary collection (ergo, a list of ListViewItems) and then remove them. You're not in the collection of ListView items when you do that so no harm. And to throw in yet another way, you could also create a temporary List(Of Integer) and go through the list like I showed and, if the item is selected, add that index number to the List(Of Integer).
Now go through that list and use .RemoveAt(i) ... that would work also.
I'll look back in here in the morning but I hope this has helped some.
Please call me Frank :)
Monday, December 31, 2012 1:15 AM -
OK, Maybe I did not pose the question correctly. I am not talking about any one clicking a button to determined the item to be remove. My written code is searching the listview to determined what entry to remove. Once my code has determined the item to be remove how do I get the index number of the item? No button click.
Ed.
ecb
Monday, December 31, 2012 4:52 AM -
Maybe you could post your code so we could see how it determines what Item is to be removed and then provide you with an answer of how to find the index number.
You've taught me everything I know but not everything you know.
Monday, December 31, 2012 4:58 AM -
Public Class Form1 Dim LViewToDelete As Integer = 10000000 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ListView1.AllowColumnReorder = True ListView1.GridLines = True ListView1.View = View.Details ListView1.Sorting = SortOrder.Ascending ListView1.Columns.Add("Instance", 200, HorizontalAlignment.Left) ListView1.Columns.Add("Instance Nr 1", 200, HorizontalAlignment.Left) ListView1.Columns.Add("Instance Nr 2", 200, HorizontalAlignment.Left) ListView1.Columns.Add("Instance Nr 3", 200, HorizontalAlignment.Left) Dim item1 As New ListViewItem("1st instance") item1.SubItems.Add("1st instance 1") item1.SubItems.Add("1st instance 2") item1.SubItems.Add("1st instance 3") ListView1.Items.AddRange(New ListViewItem() {item1}) Dim item2 As New ListViewItem("2nd instance") item2.SubItems.Add("2nd instance 1") item2.SubItems.Add("2nd instance 2") item2.SubItems.Add("2nd instance 3") ListView1.Items.AddRange(New ListViewItem() {item2}) Timer1.Interval = 2000 Timer1.Start() End Sub Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) ListView1.FindItemWithText(TextBox1.Text).Index.ToString() End Sub Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged End Sub Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then Try LViewToDelete = CInt(ListView1.FindItemWithText(TextBox1.Text).Index.ToString()) Catch ex As Exception End Try If LViewToDelete <> 10000000 Then Try ListView1.Items.RemoveAt(LViewToDelete) Catch ey As Exception End Try End If End If LViewToDelete = 10000000 End Sub End Class
You've taught me everything I know but not everything you know.
- Marked as answer by Mark Liu-lxf Monday, January 7, 2013 2:54 AM
Monday, December 31, 2012 6:14 AM -
Subitems is not a member of ListView1.
You've taught me everything I know but not everything you know.
Monday, December 31, 2012 8:52 AM -
Ed,
That is probably no true code, from1?????????????????????????????????
However, I've written this in this forum endless times, people want for some reason use a listview like a DataGridView, that goes wrong as soon as you start doing the things you want to do.
Success
Cor- Proposed as answer by Mr. Monkeyboy Monday, December 31, 2012 9:07 AM
- Marked as answer by Mark Liu-lxf Monday, January 7, 2013 2:54 AM
Monday, December 31, 2012 9:01 AM -
you can try just few lines:
Dim ListItem As ListViewItem = listView1.FindItemWithText("test") Dim index As Integer = ListItem.Index listView1.Items.RemoveAt(index) ' ------------OR------------ listView1.Items.Remove(ListItem)
thanks
Hirendra Sisodiya from authorcode.com
- Proposed as answer by JohnWein Monday, December 31, 2012 10:34 AM
- Marked as answer by Mark Liu-lxf Monday, January 7, 2013 2:54 AM
Monday, December 31, 2012 9:09 AM -
Monkeyboy, this is the code that locate the item I wish to remove from the listview. I am working with two listviews and I have already removed the item from the first listview and I wish to keep them in synch.
For each LVI2 as listviewitem in form1.listview1.items
If LVI.text = LVI2.text and _
LVI.subitems(1).text = LVI2.subitems(1).text and _
LVI.subitems(2)text = LVI2.subitems(2).text then ' <=== here I get my match of both items
from1.listview1.items.removeat(XXXXXXXX) ' <=== here is where I need the index number to remove the item
Unless I can remove it without the index. I am new in Visual Basic.
Ed.
ecb
Ed,
There's nothing wrong with using For Each, but if you do then you'll need to come up with your own counter. As an example:
Dim idx As Integer = 0
For Each lvi As ListViewItem In MyListView.Items
'Make your comparison and if true use Exit For here
idx += 1
NextNow the integer idx is the index. Another way you could do it would be:
Dim idx As Integer = 0
For i As Integer = 0 To MyListView.Items.Count - 1
' Make your comparison and if true do the following
idx = i
Exit For
NextPlease call me Frank :)
Monday, December 31, 2012 9:13 AM -
Ed,
That is probably no true code, from1?????????????????????????????????
However, I've written this in this forum endless times, people want for some reason use a listview like a DataGridView, that goes wrong as soon as you start doing the things you want to do.
The ListView and DataGridView serve different purposes. Use a DataGridView for displaying data from a bound data base. Use a ListView for unbound data. The ListView is simpler and easier to use for unbound data.
Monday, December 31, 2012 10:33 AM -
The ListView and DataGridView serve different purposes. Use a DataGridView for displaying data from a bound data base. Use a ListView for unbound data. The ListView is simpler and easier to use for unbound data.
John,
That was my idea in past also, but in fact is the DataGridView a kind of combination between the older MSFlexGrid and the DataGrid.
Both are not completely done in the DataGridView by the way, but the DataGridView works perfectly even using the designer with unbound data.
The listview is for me currently only good usable with all kind of files, because those can then be displayed as image, tile, list, detailed or even more inside the listview; And are as item easily to copy to the clipboard.
If it is to show data bound or unbound the datagridview does a better job.
Success
Cor- Edited by Cor Ligthert Monday, December 31, 2012 11:17 AM
Monday, December 31, 2012 11:15 AM -