DragDrop in TreeView

Discussion in 'C#' started by shabbir, Aug 30, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    When I started on Drag and Drop in C# I could not find much on the net and so I have decided to tackle the problem here. First follow the steps to see the working sample.

    1. Create a new C sharp windows application. Rename Form1 to a suitable name. I would refer it as TestForm
    2. Add a treeview control with some dummy nodes randomly.
    3. Extract DragDropTreeView.cs from the attached zipped file and copy DragDropTreeView.cs to the project directory.
    4. Add DragDropTreeView.cs as an Existing item to the project
    5. Go to TestForm.Designer.cs
    6. Add
    Code:
    using DragDropTreeSample;
    at the top of the file.
    7. Now find
    Code:
    private System.Windows.Forms.TreeView treeView1;
    and replace with
    Code:
    private DragDropTreeView treeView1;
    8. Go to InitializeComponent function and find
    Code:
    this.treeView1 = new System.Windows.Forms.TreeView();
    and replace with
    Code:
    this.treeView1 = new DragDropTreeView();
    Thats it. Simple isnt it. All done and now you will see that your tree view control has the drag and drop facility with the nodes you have added. Use Ctrl Key to copy a node or move the nodes in the tree using mouse drag.

    Now the question comes is what is in DragDropTreeView.cs so that it behaves in such a fashion.

    First thing that it does is sets the property AllowDrop of the DragDropTreeView to true so that we can handle the drag drop events for the TreeView control.

    Next it overrides the 3 basic or you can say minimal drag drop event handler of TreeView. i.e. OnDragDrop, OnDragOver, OnItemDrag
    Code:
    protected override void OnItemDrag(ItemDragEventArgs e)
    {
    	srcNodeForDrag = (TreeNode)e.Item;
    	this.DoDragDrop(e.Item, DragDropEffects.All);
    }
    When any item is dragged we store the source node so that we can operate on the node and then start the DragDrop operation.
    Code:
    protected override void OnDragOver(DragEventArgs e)
    {
        if ((e.KeyState & 8) == 8 &&
            (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
        {
            // CTL KeyState for copy.
            e.Effect = DragDropEffects.Copy;
        }
        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
        {
            e.Effect = DragDropEffects.Move;
        }
    }
    When any item is drag over other item we check the keyboard if we CTRL is pressed we set the copy mode of the nodes or else its the move mode.
    Code:
    protected override void OnDragDrop(DragEventArgs e)
    {
        Point pt = PointToClient(new Point(e.X, e.Y));
        TreeNode DestinationNode = this.GetNodeAt(pt);
    
        // Ensure that the list item index is contained in the data.
        if (e.Data.GetDataPresent(typeof(TreeNode)))
        {
            // Perform drag-and-drop, depending upon the effect.
            if (e.Effect == DragDropEffects.Copy)
            {
                TreeNode tmpTN = (TreeNode)srcNodeForDrag.Clone();
                DestinationNode.Nodes.Add(tmpTN);
            }
            else if (e.Effect == DragDropEffects.Move)
            {
                TreeNode tmpTN = (TreeNode)srcNodeForDrag.Clone();
                DestinationNode.Nodes.Add(tmpTN);
                srcNodeForDrag.Remove();
            }
        }
    }
    Here we check the Data type of the drop element and depending on the operation in the drag over we copy/move the node at the destination node.
     

    Attached Files:

  2. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,

    I have seen your code regarding drag and drop operations in a treeview.It is adding as a child at a dragged position.But i need to move nodes up and down with in a same level(drag and dropping of nodes at required position at a same level).

    I need this code very urgent.I will be waiting for your reply......

    Thanking You Sir........ :)
     
  3. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,

    I have seen your code regarding drag and drop operations in a treeview.It is adding as a child at a dragged position.But i need to move nodes up and down with in a same level(drag and dropping of nodes at required position at a same level).

    I need this code very urgent.I will be waiting for your reply......

    Thanking You Sir........
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Do you mean you need to add the node as its siblings to the node where the drop has taken place.

    If thats the case modify OnDragDrop to find the parent node of the DestinationNode and add it to the child of the parent node.
     
  5. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,

    I am very thankful for giving the suggestion.


    My problem is to add the dragged node as a next node(immediate sibling),at dropping position which are at same level.

    OR

    I need code,such that when we a right-click on a node,the context menu has controls for moving the selected node up and down.


    I will be waiting for your esteemed reply........


    Thanking You Sir........
     
  6. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    --------------------------------------------------------------------------------

    Dear Sir,

    I am very thankful for giving the suggestion.


    My problem is to add the dragged node as a next node(immediate sibling),at dropping position which are at same level with in a single tree.

    OR

    I need code,such that when we a right-click on a node,the context menu has controls for moving the selected node up and down with in a single tree.


    I will be waiting for your esteemed reply........


    Thanking You Sir........
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    After you get the parent where you would like to add node you can do that easily. Just try it out.
    The above case also applies to the nodes to move using the menu or something similar.
     
  8. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,

    With help of your advice ,I can move the node.But it is adding as a last child to the parent. I want to place the dragged node at dropping position itself.


    I want help on this context.


    Please kindly reply me.

    I will be waiting for your reply.......


    Thanking You Sir.........
     
  9. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    --------------------------------------------------------------------------------

    Dear Sir,

    With help of your advice ,I can move the node.But it is adding as a last child to the parent. I want to place the dragged node at dropping position itself.


    I want help on this context.


    Please kindly reply me.

    I will be waiting for your reply.......


    Thanking You Sir.........
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You need to move the node to the needed position as Add does not have any overloaded which can add a specified position of the parent node.
     
  11. sivaprakash

    sivaprakash New Member

    Joined:
    Nov 29, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Is it possible to show the node in a color currently the mouse over on it
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Its definitely possible. Override the paint and paint the node using different color
     
  13. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,

    With your help,I can able to place the nodes at required positions.But I can't save the file.How can I save it?If I saves it 80% of my work will be complete.

    Please kindly help me on this context.....

    I will be waiting for your reply...

    Thanking You Siir.........
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Which file you are trying to save??
     
  15. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,

    Actually I am working on XML File.I am representing an XML File in a treeview.After implementing required operatons(moving of nodes) ,eventhough I am giving a statement to save the file,the changes are not appearing in the original file.

    And one more thing is ,I am not able to scroll treeview while performing Drag And Drop Operations.


    KIndly help me on these two things.......

    Waiting for your reply........


    Thanking You Sir...........
     
  16. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You need to provide some more details about the xml file so that we can understand how you are reading/writing and why the changes are not happening.

    Regarding scrolling while you are dragging it should work with the mouse and not with the mouse scroll as with any other normal window/controls.
     
  17. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,

    I am moving moving the nodes of an xml file in a treeview to the desired positions.I can move nodes in a treeview to a desired position.These changes(new positions for nodes) can be seen in a treeview.But even though I am saving the file after moving the nodes,I am not able to see those changes,when we load the file next time.


    I am not understanding regarding of scrolling.


    Waiting for your reply..................

    Thanking You Sir............
     
  18. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Regarding scrolling it should be behaving as any other normal window tree view does as we dont handle or interrupt anything in the tree view.
     
  19. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,

    I am moving moving the nodes of an xml file in a treeview to the desired positions.I can move nodes in a treeview to a desired position.These changes(new positions for nodes) can be seen in a treeview.But even though I am saving the file after moving the nodes,I am not able to see those changes,when we load the file next time.





    Waiting for your reply..................

    Thanking You Sir............
     
  20. viswa56@gmail.com

    viswa56@gmail.com New Member

    Joined:
    Nov 28, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,

    Can I implement autoscrolling in the treeview which is presented by you?If so how can I?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice