注册 登录
编程论坛 VB.NET论坛

ListView数据复制

dunnys 发布于 2022-02-13 16:01, 1358 次点击
你好

下面这段代码是ListView控件的数据复制,但每次只能复制一行,有哪位大哥可以帮忙修改为可以每次复制多行数据。


 If ListView2.SelectedItems.Count <= 0 Then
            Return
        End If
        Dim Linedetails As ListView.SelectedListViewItemCollection = Me.ListView2.SelectedItems
        Dim item As ListViewItem
        Dim itemarray(6) As String
        For Each item In Linedetails
            For i = 0 To item.SubItems.Count - 1
                If i = 0 Then
                    ReDim itemarray(item.SubItems.Count)
                End If
                itemarray(i) = CStr(item.SubItems(i).Text)
            Next
        Next
        With
            .Clear()
            .SetText(Join(itemarray, vbTab))
        End With
2 回复
#2
apull2022-02-13 20:29
ReDim itemarray(item.SubItems.Count)
for循环中每次都把这个数组清空了,放到剪贴板的时候只保留了最后一组数据。
可以改用个字符串变量保存选中内容

程序代码:

If ListView1.SelectedItems.Count <= 0 Then
    Return
End If
'Dim Linedetails As ListView.SelectedListViewItemCollection = Me.ListView1.SelectedItems
'
Dim item As ListViewItem
'
Dim itemarray(6) As String
Dim str As String = ""
For Each item In ListView1.SelectedItems
    For i = 0 To item.SubItems.Count - 1
        'If i = 0 Then
        '    ReDim itemarray(item.SubItems.Count)
        'End If
        'itemarray(i) = CStr(item.SubItems(i).Text)
        str &= CStr(item.SubItems(i).Text) & vbTab
    Next
    str &= vbCrLf
Next

With '    .Clear()
    .Clear()
    '.SetText(Join(itemarray, vbTab))
    .SetText(str)

End With


[此贴子已经被作者于2022-2-13 20:33编辑过]

#3
dunnys2022-02-14 09:53
谢谢,现在已经可以复制多条数据。
1