Results 1 to 4 of 4

Thread: TreeView Drag Drop

  1. #1

    Thread Starter
    Fanatic Member khalik_ash's Avatar
    Join Date
    Aug 2002
    Location
    Singapore
    Posts
    724

    TreeView Drag Drop

    hi

    i need to perform drag and drop operation

    TreeView --- TreeView
    TreeView --- ListView
    ListView --- TreeView

    so one at a time... i have tried a bit but seem to have a problem...


    Code:
    Private Sub TV_ORG_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TV_ORG.DragEnter
            e.Effect = DragDropEffects.Move
        End Sub
    
    Private Sub TV_ORG_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TV_ORG.ItemDrag
            DoDragDrop(e.Item, DragDropEffects.Move)
        End Sub
    
        Private Sub TV_ORG_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TV_ORG.DragDrop
            Dim Nd As TreeNode
            Try
                Dim newNode As TreeNode = New TreeNode(e.Data.GetData("System.Windows.Forms.TreeNode"))
                If Nd.Equals(newNode) = False Then
                    Nd.Nodes.Add(New TreeNode(newNode.Clone))
                    newNode.Remove()
                End If
            Catch ex As Exception
                StatusBar1.Panels(0).Text = ex.Message
            End Try
        End Sub
    
    the problem area look like  when i debug it...
    
                Dim newNode As TreeNode = New TreeNode(e.Data.GetData("System.Windows.Forms.TreeNode"))
    
    the error i get is
    Cast fro TreeNode to String is not Valid

  2. #2

    Thread Starter
    Fanatic Member khalik_ash's Avatar
    Join Date
    Aug 2002
    Location
    Singapore
    Posts
    724
    welll

    nothing ...just nothing...

  3. #3
    Member
    Join Date
    Jul 2003
    Location
    London
    Posts
    44

    Try this for dragging from Treeview1 to Treeview2

    Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag

    Dim nodX As TreeNode
    'Gets the text from the node and passes it to the DragDrop Procedure
    nodX = CType(e.Item, TreeNode)
    DoDragDrop(nodX.Text, DragDropEffects.Copy)
    End Sub



    Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown

    'Gets the node from the position the MouseDown Event was started
    Dim NodX As TreeNode = TreeView1.GetNodeAt(New Drawing.Point(e.X, e.Y))
    'Sets the node to be selected
    TreeView1.SelectedNode = NodX
    End Sub



    Private Sub TreeView2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView2.DragEnter

    e.Effect = DragDropEffects.Copy
    End Sub



    Private Sub TreeView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView2.DragDrop

    'Creates a new Treenode, sets the text of the Treenode to the Text from the DropEvent Argument
    Dim nodX As New TreeNode(e.Data.GetData(DataFormats.Text))
    TreeView2.Nodes.Add(nodX)
    End Sub



    This should work in the same way for a ListView as well. Just substitute a ListItem for the TreeNode.

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you could also do it like this :
    VB Code:
    1. Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag
    2.         Dim myNode As TreeNode = DirectCast(e.Item, TreeNode)
    3.         DoDragDrop(myNode.Text, DragDropEffects.Copy)
    4.     End Sub
    5.  
    6.     Private Sub TreeView2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView2.DragEnter
    7.         e.Effect = DragDropEffects.Copy
    8.     End Sub
    9.  
    10.     Private Sub TreeView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView2.DragDrop
    11.         TreeView2.Nodes.Add(e.Data.GetData(DataFormats.Text))
    12.     End Sub
    hope it helps
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width