0

i wonder it there is any property in the treeview that allows the user to move nodes up and down using the mouse drag in run time (like swapping two nodes without need to add extra buttons)

1 Answer 1

0

Here is the code of this link drag nodes in treeview
it can be used to drag nodes from tree to another one but any one can customize it to be in the same tree

Create a new Windows Application in Visual C#. By default, a form that is named Form1 is created. Use the toolbox to add two TreeView controls to Form1. By default, TreeView1 and TreeView2 are created. To allow the TreeView controls to be dragged and dropped, change the AllowDrop property of both TreeView1 and TreeView2 to True in the Properties window. Double-click Form1 to generate the method handler for Form1's Load event. Add the following code to populate the two TreeView controls with TreeNode objects and to define the event handlers:

Add the following method handler to handle the ItemDrag event of either TreeView1 or TreeView2, depending on the direction of the drag operation. This code initiates a move operation on the item that is being dragged. private void treeView_ItemDrag(object sender,

Add the following method handler to handle the DragEnter event of TreeView1 or TreeView2, depending on the direction of the drag operation: private void treeView_DragEnter(object sender,

Add the following method to the Form1 class so that it immediately follows the method that you added in the previous step

private void form1_Load(object sender, EventArgs e)
    {
        this.treeView.ItemDrag += new ItemDragEventHandler(this.treeView_ItemDrag);
        this.treeView.DragDrop += new DragEventHandler(this.treeView_DragDrop);
        this.treeView.DragEnter += new DragEventHandler(this.treeView_DragEnter);
    }
    private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
    {
        DoDragDrop(e.Item, DragDropEffects.Move);
        _selectedNode = (TreeNode)e.Item;
    }
    private void treeView_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }
    private void treeView_DragDrop(object sender, DragEventArgs e)
    {
        TreeNode sourceNode = _selectedNode;
        if (sourceNode != null)
        {
            if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
            {
                Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
                TreeNode destinationNode = ((TreeView)sender).GetNodeAt(pt);
                if (destinationNode != null)
                {
                    //ur target
                }
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.