Results 1 to 3 of 3

Thread: How to do a multiple-row copy in a listview ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    west
    Posts
    96

    How to do a multiple-row copy in a listview ?

    Hi everybody

    I have a piece of code from a guy in this forum and it works well for my purpose. However, i have some questions to ask in order to make this piece of code works even better. The problem that i am facing is: i can't copy multiple rows of data to any available rows within the listview. When i multi select the rows, right clicked and choose copy, the code can only allow me to copy one row of data to anyone row within the listview although i have multi select the rows of data. I have attached a sceen capture for your reference.The code is as follows:

    Private _listviewContextMenu As ContextMenu
    Private _listviewItemToCutOrCopy As ListViewItem
    Private _listviewItemToCutOrCopyIndex As Integer
    Private _listviewAction As ListviewAction

    Private Sub ListView4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView4.SelectedIndexChanged
    _listviewContextMenu = New ContextMenu()
    _listviewContextMenu.MenuItems.Add("Copy", New EventHandler(AddressOf _listviewContextMenu_Copy))
    _listviewContextMenu.MenuItems.Add("Paste - Overwriting Item", New EventHandler(AddressOf _listviewContextMenu_Paste))
    ListView4.ContextMenu = _listviewContextMenu
    End Sub

    Private Sub _listviewContextMenu_Copy(ByVal sender As Object, ByVal e As System.EventArgs)
    ' clicked somewhere odd:
    If ListView4.SelectedItems.Count = 0 Then Exit Sub
    ' store the index of the selected item.
    ' unlock paste option
    _listviewItemToCutOrCopy = ListView4.SelectedItems(0).Clone
    _listviewItemToCutOrCopyIndex = ListView4.SelectedItems(0).Index
    _listviewAction = ListviewAction.Copying
    End Sub

    Private Sub _listviewContextMenu_Paste(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim i As Integer
    ' clicked somewhere odd:
    If ListView4.SelectedItems.Count = 0 Then Exit Sub
    'drop on self:
    If ListView4.SelectedItems(0).Index = _listviewItemToCutOrCopyIndex Then Exit Sub

    Select Case _listviewAction
    Case ListviewAction.Cutting
    Dim toLvi As ListViewItem = ListView4.SelectedItems(0)
    For i = 1 To toLvi.SubItems.Count - 1
    ' this starts at the first item, not the first subitem ?!
    toLvi.SubItems(i).Text = _listviewItemToCutOrCopy.SubItems(i).Text
    Next
    ListView4.Items.RemoveAt(_listviewItemToCutOrCopyIndex)
    ' may paste again, but don't cut again:
    _listviewAction = ListviewAction.Copying
    Case ListviewAction.Copying
    ' this will overwrite an existing listview item, you might want to insert instead
    Dim toLvi As ListViewItem = ListView4.SelectedItems(0)
    For i = 1 To toLvi.SubItems.Count - 1
    toLvi.SubItems(i).Text = _listviewItemToCutOrCopy.SubItems(i).Text
    Next
    ' may paste again, but don't cut again:
    _listviewAction = ListviewAction.Copying
    End Select
    End Sub
    Attached Images Attached Images  
    Last edited by chioman; Jan 13th, 2006 at 02:34 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to do a multiple-row copy in a listview ?

    First off, please use [code] tags when posting code. Secondly, the code you have posted specifically copies the item at index zero in the SelectedItems. If you want to copy every selected item then logically you need to perform the same operation on the items at every index. If you want to perform an operation on every item in a collection then you normally use a For or For Each loop.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    west
    Posts
    96

    Re: How to do a multiple-row copy in a listview ?

    Can i do the followings: The one in bold is added by me....as for the paste function, i am not very sure of how to paste those rows that the user have selected to any other available rows, please advice......thank you very much.

    VB Code:
    1. Private Sub ListView4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView4.SelectedIndexChanged
    2. _listviewContextMenu = New ContextMenu()
    3. _listviewContextMenu.MenuItems.Add("Copy", New EventHandler(AddressOf _listviewContextMenu_Copy))
    4. _listviewContextMenu.MenuItems.Add("Paste - Overwriting Item", New EventHandler(AddressOf _listviewContextMenu_Paste))
    5. ListView4.ContextMenu = _listviewContextMenu
    6. End Sub
    7.  
    8. Private Sub _listviewContextMenu_Copy(ByVal sender As Object, ByVal e As System.EventArgs)
    9. Dim i As Integer
    10. ' clicked somewhere odd:
    11. If ListView4.SelectedItems.Count = 0 Then Exit Sub
    12. ' store the index of the selected item.
    13.  
    14. [B]For i = 0 To  ListView4.SelectedItems.Count - 1
    15. _listviewItemToCutOrCopy = ListView4.SelectedItems(i).Clone
    16. _listviewItemToCutOrCopyIndex = ListView4.SelectedItems(i).Index
    17. Next
    18. _listviewAction = ListviewAction.Copying[/B]
    19. End Sub
    20.  
    21. Private Sub _listviewContextMenu_Paste(ByVal sender As Object, ByVal e As System.EventArgs)
    22. Dim i As Integer
    23.  
    24.  
    25. ' clicked somewhere odd:
    26. If ListView4.SelectedItems.Count = 0 Then Exit Sub
    27. 'drop on self:
    28. If ListView4.SelectedItems(0).Index = _listviewItemToCutOrCopyIndex Then Exit Sub
    29.  
    30. Select Case _listviewAction
    31. Case ListviewAction.Cutting
    32. Dim toLvi As ListViewItem = ListView4.SelectedItems(0)
    33. For i = 1 To toLvi.SubItems.Count - 1
    34. ' this starts at the first item, not the first subitem ?!
    35. toLvi.SubItems(i).Text = _listviewItemToCutOrCopy.SubItems(i).Text
    36. Next
    37. ListView4.Items.RemoveAt(_listviewItemToCutOrCopyIndex)
    38. ' may paste again, but don't cut again:
    39. _listviewAction = ListviewAction.Copying
    40.  
    41. Case ListviewAction.Copying
    42. ' this will overwrite an existing listview item, you might want to insert instead
    43. Dim toLvi As ListViewItem = ListView4.SelectedItems(i)
    44. For i = 1 To toLvi.SubItems.Count - 1
    45. toLvi.SubItems(i).Text = _listviewItemToCutOrCopy.SubItems(i).Text
    46. Next
    47. ' may paste again, but don't cut again:
    48. _listviewAction = ListviewAction.Copying
    49. End Select
    50. End Sub
    Last edited by chioman; Jan 13th, 2006 at 10:54 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width