Help with dragging from listview to treeview

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QW5kcmV3?=

    Help with dragging from listview to treeview

    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.T reeNode
    Dim imgIndex As Windows.Forms.T reeNode
    If e.Data.GetDataP resent("System. Windows.Forms.T reeNode", False) Then
    Dim pt As Drawing.Point
    Dim destinationNode As Windows.Forms.T reeNode
    pt = CType(sender, Windows.Forms.T reeView).PointT oClient(New
    Drawing.Point(e .X, e.Y))
    destinationNode = CType(sender,
    Windows.Forms.T reeView).GetNod eAt(pt)
    NewNode = CType(e.Data.Ge tData("System.W indows.Forms.Tr eeNode"),
    Windows.Forms.T reeNode)
    If Not destinationNode .Equals(NewNode ) Then

    'now adding to the shs tree view
    destinationNode .Nodes.Add(CTyp e(NewNode.Clone ,
    Windows.Forms.T reeNode))
    imgIndex = destinationNode
    imgIndex.ImageI ndex = 2
    imgIndex.Select edImageIndex = 2
    'imgIndex.Tag = "U" & strStatus
    destinationNode .Expand()

    End If
    End If
  • Phill W.

    #2
    Re: Help with dragging from listview to treeview

    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.ListView Item) here

    If e.Data.GetDataP resent("System. Windows.Forms.T reeNode", False) Then

    ' Isn't it annoying there's no e.Location?
    Dim pt As Drawing.Point _
    = tv.PointToClien t(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.Ge tData("System.W indows.Forms.Tr eeNode") _
    , 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 _
    = destinationNode s.Nodes.Add( NewNode.Clone() )
    NewNode = destinationNode .Nodes( newIndex )

    With NewNode
    .ImageIndex = 2
    .SelectedImageI ndex = 2
    End With

    destinationNode .Expand()

    End If
    End If
    End Sub

    HTH,
    Phill W.

    Comment

    Working...