• Welcome to Valhalla Legends Archive.
 

Copying ListView Rows

Started by ObsidianWolf, January 28, 2004, 04:07 PM

Previous topic - Next topic

ObsidianWolf

Im trying to Copy a Select Row in a ListView to another ListView.

I currently have it broken down into a few functions.

This Grabs the selected data from the Desired List View

Public Function RowToStr(lstControl As ListView) As Variant
   For n = 1 To lstControl.SelectedItem.ListSubItems.Count
       RowToStr = RowToStr & Chr(255) & lstControl.SelectedItem.ListSubItems.Item(n)
   Next n
End Function


Then I call

Dim HostArray() as String
HostArray = Split(RowToStr(ListView1),Chr(255))
   ListAdd Target_lstControl, HostArray(0), HostArray(1), HostArray(2), HostArray(3), HostArray(4), HostArray(5), HostArray(6), HostArray(7), HostArray(8), HostArray(9), HostArray(10)



Where ListAdd is....

Public Sub ListAdd(lstControl As ListView, ParamArray myFields() As Variant)
   If Not lstControl.ColumnHeaders.Count > UBound(myFields) Then MsgBox "Too Many Fields Entered For this List View": End
   With lstControl.ListItems.Add(, , myFields(0))
   For n = 1 To UBound(myFields)
       .SubItems(n) = myFields(n)
   Next n
   End With
End Sub


It works fine and all but I was wondering if there is a faster way to just copy a specified row to another list view.

Fixed some spelling errors and information I left out


Grok

#1
Try setting an Item to the row you want copied, then adding that Item to the destination ListView.  If the ListViews are equivalent in structure (same number of subitems), it might work.  I haven't tried it but it should take you three minutes to test.

Hmm, I test it.  Doesn't work.  Coming up with something that does.

OK this works.  Tested with two listviews having the same number of columnheaders.

Private Sub cmdCopySelected_Click()
   Dim Item1 As MSComctlLib.ListItem
   Dim Item2 As MSComctlLib.ListItem
   Dim lPos As Long
   Set Item1 = ListView1.SelectedItem
   Set Item2 = ListView2.ListItems.Add(, Item1.Key, Item1.Text, Item1.Icon, Item1.SmallIcon)
   For lPos = 1 To ListView1.ColumnHeaders.Count - 1
       Item2.SubItems(lPos) = Item1.SubItems(lPos)
   Next
End Sub

ObsidianWolf

Most Excellent, Thanks for the suggestion.

Grok

Quote from: ObsidianWolf on January 29, 2004, 12:53 AM
Most Excellent, Thanks for the suggestion.

You're welcome.  Where's my dang +1 ?? :P