Drag Telerik RadTreeview node to Textbox in C#

1k views Asked by At

I want to drag a node from Telerik RadTreeviw to my Textbox in C# winform. I looked at this solution but it's for Microsoft Treeview and it's not working on Telerik, because RadTreeview just gets messy with this part

DoDragDrop(e.Node.Text, DragDropEffects.Copy);

Does anyone knows a working solution in my case for Telerik RadTreeView?

2

There are 2 answers

1
checho On BEST ANSWER

Here is how to drag a node from RadTreeView to RadTextBox (can be replaced with the standard TextBox too, as in fact HostedTextBox is the standard one)

public partial class Form1 : Form
{
    Point clickedPoint;
    bool mouseDown = false;

    public Form1()
    {
        InitializeComponent();

        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.AllowDrop = true;
        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.DragEnter += new DragEventHandler(HostedControl_DragEnter);
        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.DragDrop += new DragEventHandler(HostedControl_DragDrop);

        radTreeView1.MouseDown += new MouseEventHandler(radTreeView1_MouseDown);
        radTreeView1.MouseMove += new MouseEventHandler(radTreeView1_MouseMove);

    }

    void HostedControl_DragDrop(object sender, DragEventArgs e)
    {
        RadTreeNode node = e.Data.GetData(typeof(RadTreeNode)) as RadTreeNode;
        if (node != null)
        {
            radTextBox1.Text = node.Text;
        }
    }

    void HostedControl_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    void radTreeView1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mouseDown && IsRealDrag(e.Location, clickedPoint))
        {
            TreeNodeElement node = ((RadTreeView)sender).ElementTree.GetElementAtPoint(clickedPoint) as TreeNodeElement;
            if (node != null)
            {
                ((RadTreeView)sender).DoDragDrop(node.Data, DragDropEffects.Copy | DragDropEffects.Move);
            }
            mouseDown = false;
        }
    }

    void radTreeView1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            mouseDown = true;
            clickedPoint = e.Location;
        }
    }

    private static bool IsRealDrag(Point mousePosition, Point initialMousePosition)
    {
        return (Math.Abs(mousePosition.X - initialMousePosition.X) >= SystemInformation.DragSize.Width) ||
            (Math.Abs(mousePosition.Y - initialMousePosition.Y) >= SystemInformation.DragSize.Height);
    }
}
0
Ghasem On

I added some codes to @checho's so that I can drag & drop nodes in my RadTreeView without having to set its AllowDragDrop=true.

I added two RadioButtons to my form to change Copy and Cut mode when dragging between nodes.

Here's my code

public partial class Form1 : Form
{
    Point clickedPoint;
    bool mouseDown = false;

    public Form1()
    {
        InitializeComponent();

        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.AllowDrop = true;
        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.DragEnter += new DragEventHandler(HostedControl_DragEnter);
        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.DragDrop += new DragEventHandler(HostedControl_DragDrop);

        radTreeView1.MouseDown += new MouseEventHandler(radTreeView1_MouseDown);
        radTreeView1.MouseMove += new MouseEventHandler(radTreeView1_MouseMove);
    radTreeView1.MouseMove += new MouseEventHandler(radTreeView1_DragEnter);
    radTreeView1.MouseMove += new MouseEventHandler(radTreeView1_DragDrop);

    }

    void HostedControl_DragDrop(object sender, DragEventArgs e)
    {
        RadTreeNode node = e.Data.GetData(typeof(RadTreeNode)) as RadTreeNode;
        if (node != null)
        {
            radTextBox1.Text = node.Text;
        }
    }

    void HostedControl_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    void radTreeView1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mouseDown && IsRealDrag(e.Location, clickedPoint))
        {
            TreeNodeElement node = ((RadTreeView)sender).ElementTree.GetElementAtPoint(clickedPoint) as TreeNodeElement;
            if (node != null)
            {
                ((RadTreeView)sender).DoDragDrop(node.Data, DragDropEffects.Copy | DragDropEffects.Move);
            }
            mouseDown = false;
        }
    }

    void radTreeView1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            mouseDown = true;
            clickedPoint = e.Location;
        }
    }

    private static bool IsRealDrag(Point mousePosition, Point initialMousePosition)
    {
        return (Math.Abs(mousePosition.X - initialMousePosition.X) >= SystemInformation.DragSize.Width) ||
            (Math.Abs(mousePosition.Y - initialMousePosition.Y) >= SystemInformation.DragSize.Height);
    }

    private void radTreeView1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = radioButtonCopy.Checked ? DragDropEffects.Copy : DragDropEffects.Move;
    }

    private void radTreeView1_DragDrop(object sender, DragEventArgs e)
    {

            RadTreeNode sourceNode = e.Data.GetData(typeof(RadTreeNode)) as RadTreeNode;
            // Retrieve the client coordinates of the drop location.
            Point targetPoint = rtvTree.PointToClient(new Point(e.X, e.Y));

            // Retrieve the node at the drop location.
            RadTreeNode targetNode = rtvTree.GetNodeAt(targetPoint);
            if (sourceNode != null && targetNode != null)
            {
                RadTreeNode clonedNode = (RadTreeNode) sourceNode.Clone();
                if (radioButtonCut.Checked)
                    sourceNode.Remove();
                targetNode.Nodes.Add(clonedNode);
                targetNode.Expanded = true;
            }
     }
}