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
Last edited by chioman; Jan 13th, 2006 at 02:34 AM.
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.
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:
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)
Dim i As Integer
' clicked somewhere odd:
If ListView4.SelectedItems.Count = 0 Then Exit Sub