Valhalla Legends Forums Archive | Visual Basic Programming | Copying ListView Rows

AuthorMessageTime
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
[code]
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
[/code]

Then I call
[code]
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)

[/code]

Where ListAdd is....
[code]
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
[/code]

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

January 28, 2004, 10:07 PM
Grok
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.

[code]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[/code]
January 29, 2004, 2:33 AM
ObsidianWolf
Most Excellent, Thanks for the suggestion.
January 29, 2004, 6:53 AM
Grok
[quote author=ObsidianWolf link=board=31;threadid=4975;start=0#msg41628 date=1075359180]
Most Excellent, Thanks for the suggestion.
[/quote]

You're welcome. Where's my dang +1 ?? :P
January 29, 2004, 2:21 PM

Search