By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,885 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,885 IT Pros & Developers. It's quick & easy.

Help with dragging from listview to treeview

P: n/a
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.TreeNo de", 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.TreeNod e"),
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
Nov 11 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Andrew wrote:
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.TreeNo de", 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.TreeNod e") _
, 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.
Nov 17 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.