Help with dragging from listview to treeview

A

Andrew

I am trying to adapt some code that I have where I drag and drop from two
treeview controls. Now I want to drag and drop from a listview to a treeview.
Whatever I do does not work and I cannot find any good articals on the net on
how to do so. I have attached my original code for the two treeview
controls. Can anybody point out where I need to make changes?


Dim NewNode As Windows.Forms.TreeNode
Dim imgIndex As Windows.Forms.TreeNode
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", False) Then
Dim pt As Drawing.Point
Dim destinationNode As Windows.Forms.TreeNode
pt = CType(sender, Windows.Forms.TreeView).PointToClient(New
Drawing.Point(e.X, e.Y))
destinationNode = CType(sender,
Windows.Forms.TreeView).GetNodeAt(pt)
NewNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"),
Windows.Forms.TreeNode)
If Not destinationNode.Equals(NewNode) Then

'now adding to the shs tree view
destinationNode.Nodes.Add(CType(NewNode.Clone,
Windows.Forms.TreeNode))
imgIndex = destinationNode
imgIndex.ImageIndex = 2
imgIndex.SelectedImageIndex = 2
'imgIndex.Tag = "U" & strStatus
destinationNode.Expand()

End If
End If
 
P

Phill W.

Andrew said:
I am trying to adapt some code that I have where I drag and drop from two
treeview controls. Now I want to drag and drop from a listview to a treeview.
Can anybody point out where I need to make changes?


Imports System.Windows.Forms

Private Sub X_Drag*( ... ) Handles X.Drag*

Dim tv as TreeView = DirectCast( sender, TreeView )

' For a ListViewItem, you need to look for a /different/ Type
' (S.W.F.ListViewItem) here

If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", False) Then

' Isn't it annoying there's no e.Location?
Dim pt As Drawing.Point _
= tv.PointToClient(New Drawing.Point(e.X, e.Y))

Dim destinationNode As TreeNode _
= tv.GetNodeAt( pt )

' You can't cast a ListViewItem into a TreeNode.
' You'll have to create a /new/ TreeNode and set it up
' based on the given ListViewItem (or TreeNode, if you
' want to support Drag-and-Drop within the Tree as well!
Dim NewNode As TreeNode _
= CType(e.Data.GetData("System.Windows.Forms.TreeNode") _
, TreeNode)

' Everything else should work as given ...

If Not destinationNode.Equals( NewNode ) Then

' If you're creating a new Node from a ListViewItem,
' the above will never be True, but anyway ...

Dim newIndex as Integer _
= destinationNodes.Nodes.Add( NewNode.Clone() )
NewNode = destinationNode.Nodes( newIndex )

With NewNode
.ImageIndex = 2
.SelectedImageIndex = 2
End With

destinationNode.Expand()

End If
End If
End Sub

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top