Sure, here is all you need. In my example, I have a treeview (tvFolders) and an objectlistview (lvMessages). I want to drag-and-drop an item from the objectlistview onto the treeview and then move the file in question (class type=MyMessage)to the appropriate directory.
1) in the objectListView's ItemDrag event, put this:
Dim pt As Point = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
Dim targetNode As TreeNode = tvFolders.GetNodeAt(pt)
If targetNode IsNot Nothing Then
If e.Data.GetDataPresent("BrightIdeasSoftware.OLVListItem", True) Then
If Not tvFolders.SelectedNode Is targetNode Then
If _lastHighlightedNode IsNot Nothing Then
_lastHighlightedNode.BackColor = Color.White
End If
targetNode.BackColor = Color.Yellow
_lastHighlightedNode = targetNode
Dim dropnode As TreeNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
Do Until targetNode Is Nothing
If targetNode Is dropnode Then
e.Effect = DragDropEffects.None
Exit Sub
End If
targetNode = targetNode.Parent
Loop
e.Effect = DragDropEffects.Move
End If
End If
Else
e.Effect = DragDropEffects.None
_lastHighlightedNode.BackColor = Color.White
End If
3) In the treeview's DragDrop event, add this:
_lastHighlightedNode.BackColor = Color.White
If e.Data.GetDataPresent("BrightIdeasSoftware.OLVListItem", True) Then
'-- move
Dim strDetinationFolder As String = _lastHighlightedNode.Tag.ToString()
Dim item As BrightIdeasSoftware.OLVListItem = CType(e.Data.GetData("BrightIdeasSoftware.OLVListItem"), BrightIdeasSoftware.OLVListItem)
Dim strSource As String = CType(item.RowObject, MyMessage).Filename
Dim strDestination As String = QualifyPath(strDetinationFolder) & System.IO.Path.GetFileName(strSource)
System.IO.File.Move(strSource, strDestination)
item.Remove()
End If
End If
4) Finally, in the treeview_DragEnter event, add this:
If e.Data.GetDataPresent("BrightIdeasSoftware.OLVListItem", True) Then
'-- move
e.Effect = DragDropEffects.Move
End If
5) Oh, you will also need this at the module level if you want to do some nice highlighting
Dim _lastHighlightedNode As TreeNode
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Actually, a little trial and error and I got drag-and-drop to work. I just needed to change my references to OLVListItem from "System.Windows.Forms.ListViewItem" and a few other minor tweaks but it works fine.
I bet others would love it if you updated the sample app to include drag-and-drop.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you give me small example of how you made it work, I'll happily include it in the demo. Several people have asked for exactly this information, but I've never made it a priority to track it down.
Thanks,
Phillip
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I encountered an strange error:
Using simple drag and drop with OLV, this code does not work!
if (e.Data.GetDataPresent(typeof(OLVDataObject)))
e.Effect = DragDropEffects.Move;
What is more strange is that visual studio keeps insisting that the type of e.Data is actually OLVDataObject.
I had this same problem for another data object, but its solution is not applicable here. My other problem was that OLVDataObject was created in another DLL instance. Although the DLLs were exactly copies of themselves, the runtime type for the object was not the same. (string representations were exactly the same though!)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
OLV is great! However, I do not see how to implement drag-and-drop with it. I do not see it mentioned in the docs or in the sample app. Any ideas?
Thanks,
John
Sure, here is all you need. In my example, I have a treeview (tvFolders) and an objectlistview (lvMessages). I want to drag-and-drop an item from the objectlistview onto the treeview and then move the file in question (class type=MyMessage)to the appropriate directory.
1) in the objectListView's ItemDrag event, put this:
2) in the treeview's DragOver event, put this:
3) In the treeview's DragDrop event, add this:
4) Finally, in the treeview_DragEnter event, add this:
5) Oh, you will also need this at the module level if you want to do some nice highlighting
Actually, a little trial and error and I got drag-and-drop to work. I just needed to change my references to OLVListItem from "System.Windows.Forms.ListViewItem" and a few other minor tweaks but it works fine.
I bet others would love it if you updated the sample app to include drag-and-drop.
Hi John,
If you give me small example of how you made it work, I'll happily include it in the demo. Several people have asked for exactly this information, but I've never made it a priority to track it down.
Thanks,
Phillip
I encountered an strange error:
Using simple drag and drop with OLV, this code does not work!
if (e.Data.GetDataPresent(typeof(OLVDataObject)))
e.Effect = DragDropEffects.Move;
What is more strange is that visual studio keeps insisting that the type of
e.Data
is actually OLVDataObject.I had this same problem for another data object, but its solution is not applicable here. My other problem was that OLVDataObject was created in another DLL instance. Although the DLLs were exactly copies of themselves, the runtime type for the object was not the same. (string representations were exactly the same though!)
Hi Klaus,
When asking a question, it's generally better to create a new topic, rather than resurrecting a topic that is 6 years old! :)
Are you trying to move some OLV rows between two processes? That won't work.
e.Data
is an instance ofOLVDataObject
-- it doesn't contain one.