Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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.

ListViewItem tmpItem = (ListViewItem)lvwLeftList.SelectedItems[0].Clone();
lvwRightList.Items.Add(tmpItem);

Anybody looking for a graduate programmer (Midlands, England)?
Posted

Thanks again, but I'm using VB in VS2005. Sorry, I'll be more specific next time I create a post.

 

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.

ListViewItem tmpItem = (ListViewItem)lvwLeftList.SelectedItems[0].Clone();
lvwRightList.Items.Add(tmpItem);

Posted

Dim tmpItem As ListViewItem = CType(lvwA.SelectedItems(0).Clone(), ListViewItem)
lvwB.Items.Add(tmpItem)

You can set in your profile your language preference.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

Thanks Again Cags!!! Works great. Wasn't aware of .clone Hope the weather's great "across the pond" today!

 

Dim tmpItem As ListViewItem = CType(lvwA.SelectedItems(0).Clone(), ListViewItem)
lvwB.Items.Add(tmpItem)

You can set in your profile your language preference.

Posted

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!

 

Dim tmpItem As ListViewItem = CType(lvwA.SelectedItems(0).Clone(), ListViewItem)
lvwB.Items.Add(tmpItem)

You can set in your profile your language preference.

Posted
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...

       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

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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.

 

   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

 

 

 

 

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...

       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

Posted

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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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.

   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

 

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...