Retrieving multiple subitems from a listview

bjwade62

Centurion
Joined
Oct 31, 2003
Messages
104
I've been able to get an item and one of it's subitems from a listview and copy it to another listview. However I have more than one subitem I want to copy.

Here's what I have so far

Dim RightDate As String
RightDate = lvwLeftList.Items(I).SubItems(1).Text
lvwRightList.Items.Add(SelItem).SubItems.Add(RightDate)

Anyone know how to get the additional subitems?
 
It would be possible to loop through the subitems using a method similar to the one your using, but if you are just trying to copy an item from one to the other you would be better off doing something like this.
C#:
ListViewItem tmpItem = (ListViewItem)lvwLeftList.SelectedItems[0].Clone();
lvwRightList.Items.Add(tmpItem);
 
Thanks again, but I'm using VB in VS2005. Sorry, I'll be more specific next time I create a post.

Cags said:
It would be possible to loop through the subitems using a method similar to the one your using, but if you are just trying to copy an item from one to the other you would be better off doing something like this.
C#:
ListViewItem tmpItem = (ListViewItem)lvwLeftList.SelectedItems[0].Clone();
lvwRightList.Items.Add(tmpItem);
 
Visual Basic:
 Dim tmpItem As ListViewItem = CType(lvwA.SelectedItems(0).Clone(), ListViewItem)
lvwB.Items.Add(tmpItem)
You can set in your profile your language preference.
 
Thanks Again Cags!!! Works great. Wasn't aware of .clone Hope the weather's great "across the pond" today!

Cags said:
Visual Basic:
 Dim tmpItem As ListViewItem = CType(lvwA.SelectedItems(0).Clone(), ListViewItem)
lvwB.Items.Add(tmpItem)
You can set in your profile your language preference.
 
I spoke a little too soon. I'm having a problem getting the code to run inside a loop.

I'm using a Do loop. Similar to below. The real code has much more inside the loop but you get the idea. I changed the (lvwLeftList.SelectedItems(0) to (lvwLeftList.SelectedItems(I) because I'm trying to select multiple.

Do while I <= lvwLeftList.items.count
Dim tmpItem As ListViewItem = CType(lvwLeftList.SelectedItems(I).Clone(), ListViewItem) lvwRightList.Items.Add(tmpItem)
I=I+1
Loop

Thanks for the help Cags!

Cags said:
Visual Basic:
 Dim tmpItem As ListViewItem = CType(lvwA.SelectedItems(0).Clone(), ListViewItem)
lvwB.Items.Add(tmpItem)
You can set in your profile your language preference.
 
bjwade62 said:
I spoke a little too soon. I'm having a problem getting the code to run inside a loop.

I'm using a Do loop. Similar to below. The real code has much more inside the loop but you get the idea. I changed the (lvwLeftList.SelectedItems(0) to (lvwLeftList.SelectedItems(I) because I'm trying to select multiple.

Do while I <= lvwLeftList.items.count
Dim tmpItem As ListViewItem = CType(lvwLeftList.SelectedItems(I).Clone(), ListViewItem) lvwRightList.Items.Add(tmpItem)
I=I+1
Loop

Thanks for the help Cags!
Your loop is telling your application to iterate i untill it equals the total number of Items in the ListView. Then within your loop you are accessing items at the index i in the SelectedItems array. Unless you have highlighted all the items this will cause an Index out of bounds error. You want to use one of the following...
Visual Basic:
        Dim i As Single
        ' to copy all items
        Do While i < lvwA.Items.Count
            Dim tmpItem As ListViewItem = CType(lvwA.Items(i).Clone(), ListViewItem)
            lvwB.Items.Add(tmpItem)
            i += 1
        Loop
        ' to copy selected items
        Do While i < lvwA.SelectedItems.Count
            Dim tmpItem As ListViewItem = CType(lvwA.SelectedItems(i).Clone(), ListViewItem)
            lvwB.Items.Add(tmpItem)
            i += 1
        Loop
 
At this point I'm becoming a pain. I know. I'm sorry. Here's the problem. If I select everything in the left listview it works fine. However if I select every thing in the left but the first item it copys all selected item minus one. If I select everything in the left listview except the top two items it copys all selected items minus two and so on.

Here's me entire loop in case that helps.

Visual Basic:
    Private Sub ToolCopyRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolCopyRight.Click
        Dim I As Integer = 0
        If lvwLeftList.Items.Count > 0 Then
            Do While I <= lvwLeftList.Items.Count
                Try
                    If lvwLeftList.Items.Item(I).Selected = True Then
                        lvwLeftList.Items.Item(I).ImageIndex = 1
                        Dim SelItem As String = lvwLeftList.Items.Item(I).Text
                        If cmboLeft.Text.Length > 3 Then
                            Dim FullPathLeft As String = cmboLeft.Text & "\" & SelItem
                            Dim FullPathRight As String = cmboRight.Text & "\" & SelItem
                            Dim ListRight As New ListViewItem
                            FileCopy(FullPathLeft, FullPathRight)
                            Me.ToolStripStatusRightShown.Text = "Files: " & lvwRightList.Items.Count + 1
                            Dim returnValue As ListViewItem = lvwRightList.FindItemWithText(SelItem)
                            If (returnValue Is Nothing) Then

                                'Do While I < lvwLeftList.SelectedItems.Count
                                Dim tmpItem As ListViewItem = CType(lvwLeftList.SelectedItems(I).Clone(), ListViewItem)
                                lvwRightList.Items.Add(tmpItem)
                                'I += 1
                                'Loop

                                lvwRightList.FindItemWithText(SelItem).ImageIndex = 1
                            Else
                                lvwRightList.FindItemWithText(SelItem).ImageIndex = 1
                            End If
                            Dim RightListCount As Integer
                            RightListCount = lvwRightList.Items.Count
                            For Each ListRight In lvwRightList.Items
                                If ListRight.ImageIndex = -1 Then
                                    ListRight.ImageIndex = 1
                                End If
                            Next
                        Else
                            Dim FullPathLeft As String = cmboLeft.Text & SelItem
                            Dim FullPathRight As String = cmboRight.Text & SelItem
                            Dim ListRight As New ListViewItem
                            FileCopy(FullPathLeft, FullPathRight)
                            Me.ToolStripStatusRightShown.Text = "Files: " & lvwRightList.Items.Count + 1
                            Dim returnValue As ListViewItem = lvwRightList.FindItemWithText(SelItem)
                            If (returnValue Is Nothing) Then

                                'Do While I < lvwLeftList.SelectedItems.Count
                                Dim tmpItem As ListViewItem = CType(lvwLeftList.SelectedItems(I).Clone(), ListViewItem)
                                lvwRightList.Items.Add(tmpItem)
                                'I += 1
                                'Loop

                                lvwRightList.FindItemWithText(SelItem).ImageIndex = 1
                            End If
                            Dim RightListCount As Integer
                            RightListCount = lvwRightList.Items.Count
                            For Each ListRight In lvwRightList.Items
                                If ListRight.ImageIndex = -1 Then
                                    ListRight.ImageIndex = 1
                                End If
                            Next
                        End If
                    End If
                Catch ex As Exception
                End Try
                I = I + 1
            Loop
            lvwLeftList.SelectedItems.Clear()
        End If
    End Sub




Cags said:
Your loop is telling your application to iterate i untill it equals the total number of Items in the ListView. Then within your loop you are accessing items at the index i in the SelectedItems array. Unless you have highlighted all the items this will cause an Index out of bounds error. You want to use one of the following...
Visual Basic:
        Dim i As Single
        ' to copy all items
        Do While i < lvwA.Items.Count
            Dim tmpItem As ListViewItem = CType(lvwA.Items(i).Clone(), ListViewItem)
            lvwB.Items.Add(tmpItem)
            i += 1
        Loop
        ' to copy selected items
        Do While i < lvwA.SelectedItems.Count
            Dim tmpItem As ListViewItem = CType(lvwA.SelectedItems(i).Clone(), ListViewItem)
            lvwB.Items.Add(tmpItem)
            i += 1
        Loop
 
Thats a lot of code and its pretty hard to understand exactly whats going on without knowing everything about your application. But heres a few notes..

1.) I'm pretty sure your application will cause an error everytime its ran since your loop is using <= count. As the items array has a zero based index this means you will be trying to access one more item than you have. Obviously this is caught by the try / catch statement but thats not idea. If you were to change this to "< lvwLeftList.Items.Count" you could also do away with the very first if statement as the loop would only be entered if there were one or more items.

2.) The reason it doesn't work properly is because you are mixing up the use of Items and SelectedItems again. You are using a counter that loops through one of the arrays and the trying to select something at that index in the other array, and hence you are getting either a crash (which is caught by the try catch) or you are simply copying the wrong item.

3.) You seem to have quite a few lines of unneccessary code, I'm sure with a bit of refactoring you could achieve the same result with perhaps little over half the amount you have.

You are looping through the Items array checking if items are selected, it would probably be much more efficient if you simply looped through the SelectedItems array.

But if you simply want to get the code working you can probably just ignore everything I've said and replace the word SelectedItems, with the word Items anywhere it appears in your code.
 
Thanks for the help today. I have it working now. You were right I had alot of code. It was all doing something, but not very efficiently. In case you're interested heres the final version. Thanks again.
Visual Basic:
    Private Sub ToolCopyRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolCopyRight.Click
        Dim I As Integer = 0
        Do While I < lvwLeftList.SelectedItems.Count
            Dim w As Integer = 0
            Do While w <= lvwLeftList.Items.Count - 1
                If lvwLeftList.Items.Item(w).Selected = True Then
                    lvwLeftList.Items.Item(w).ImageIndex = 1
                    lvwLeftList.Items.Item(w).ForeColor = Color.Blue
                    Dim SelItem As String = lvwLeftList.Items.Item(w).Text
                    Dim FullPathLeft As String = cmboLeft.Text & "\" & SelItem
                    Dim FullPathRight As String = cmboRight.Text & "\" & SelItem
                    FileCopy(FullPathLeft, FullPathRight)
                End If
                w = w + 1
            Loop
            Dim tmpItem As ListViewItem = CType(lvwLeftList.SelectedItems(I).Clone(), ListViewItem)
            lvwRightList.Items.Add(tmpItem)
            I += 1
        Loop
        lvwLeftList.SelectedItems.Clear()
    End Sub

Cags said:
Thats a lot of code and its pretty hard to understand exactly whats going on without knowing everything about your application. But heres a few notes..

1.) I'm pretty sure your application will cause an error everytime its ran since your loop is using <= count. As the items array has a zero based index this means you will be trying to access one more item than you have. Obviously this is caught by the try / catch statement but thats not idea. If you were to change this to "< lvwLeftList.Items.Count" you could also do away with the very first if statement as the loop would only be entered if there were one or more items.

2.) The reason it doesn't work properly is because you are mixing up the use of Items and SelectedItems again. You are using a counter that loops through one of the arrays and the trying to select something at that index in the other array, and hence you are getting either a crash (which is caught by the try catch) or you are simply copying the wrong item.

3.) You seem to have quite a few lines of unneccessary code, I'm sure with a bit of refactoring you could achieve the same result with perhaps little over half the amount you have.

You are looping through the Items array checking if items are selected, it would probably be much more efficient if you simply looped through the SelectedItems array.

But if you simply want to get the code working you can probably just ignore everything I've said and replace the word SelectedItems, with the word Items anywhere it appears in your code.
 
Back
Top