1 Attachment(s)
copy and paste in listview
Hi everybody
I have the following codes below on copy and paste routine within a listview and I would like to do a multiple rows of copy within the same listview and it has been achieved. However, i have a problem. I have attached a screen dump on this. The first listview shown is when the user right click and trying to copy the 3 rows starting from logical number 0 to 2 and paste to the rows starting from logical number 7. The screen dump shows that the rows of data is being copied but the logical number 7 onwards is overwrite by the logical number 0 to 2 which i don't want this to happen. This means when i do the multiple copy of rows of data, it copy the data starting with subitems(0). How do i modify the code to start with subitems(1).can anyone advice me ? thank u very much.
Code:
Private Sub _listviewContextMenu_Copy(ByVal sender As Object, ByVal e As System.EventArgs)
' clicked somewhere odd:
If ListView4.SelectedItems.Count > 0 Then
ReDim copiedLVItems(ListView4.SelectedItems.Count - 1)
End If
Dim itemIndex As Int32
For Each _listviewItemToCutOrCopy In ListView4.SelectedItems
copiedLVItems(itemIndex) = _listviewItemToCutOrCopy.Clone
itemIndex += 1
Next
_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:
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 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
' may paste again, but don't cut again:
'Alternate Paste Routine
'Ensure items are available for pasting
If Not copiedLVItems Is Nothing AndAlso copiedLVItems.Length > 0 Then
'Get currently selected index so we know where to start the paste
Dim currentIndex As Int32 = 0
If ListView4.SelectedIndices.Count > 0 Then currentIndex = ListView4.SelectedIndices.Item(ListView4.SelectedIndices.Count - 1)
Dim li As ListViewItem
Try
For Each li In copiedLVItems
ListView4.Items.Insert(currentIndex, li)
currentIndex += 1
Next
Catch
MsgBox(Err.Description) '<- will tell you the error
Finally
Dim removeLI As ListViewItem
'Remove all selected items
For Each removeLI In ListView4.SelectedItems
ListView4.Items.Remove(removeLI)
Next
End Try
End If
End Select
End Sub