RadTreeView ItemDrag mutiple times

5 posts, 1 answers
  1. Alexandre
    Alexandre avatar
    4 posts
    Member since:
    Sep 2016

    Posted 30 Jun Link to this post

    Hi,

     

    I'm using ItemDrag event to drag&drop items from a RadTreeView to another control.

    When a drag an item from the RadTreeView, the ItemDrag event is raised multiple times (While item moving) and the item is dropped multiple times on the control.

    The same code with System.Windows.Forms works fine.

    Is there a solution to achieve the same result as System.Windows.Forms ?

     

    Here is my code of a test project which compares Telerik et System.Windows.Forms:

            #region Telerik Drag&Drop

            private void radTreeView1_ItemDrag(object sender, Telerik.WinControls.UI.RadTreeViewEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("radTreeView1_ItemDrag : {0}", e.Node.Name));
                DoDragDrop(e.Node, DragDropEffects.Move);
            }

            private void radTreeView2_DragEnter(object sender, DragEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("radTreeView2_DragEnter : {0}", e.Effect));
                if (e.Data.GetDataPresent("Telerik.WinControls.UI.RadTreeNode", true) == true)
                {
                    e.Effect = DragDropEffects.Move;
                }
            }

            private void radTreeView2_DragDrop(object sender, DragEventArgs e)
            {
                RadTreeNode node = e.Data.GetData(typeof(RadTreeNode)) as RadTreeNode;
                if (node != null)
                {
                    if (e.Effect == DragDropEffects.Move)
                    {
                        System.Diagnostics.Debug.WriteLine(String.Format("radTreeView2_DragDrop : {0}", e.Effect));
                        this.radTreeView2.Nodes.Add((node.Clone() as RadTreeNode));
                    }
                }
            }

            #endregion Telerik Drag&Drop

            #region System.Windows.Forms Drag&Drop

            private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("treeView1_ItemDrag : {0}", (e.Item as TreeNode).Text));
                DoDragDrop(e.Item, DragDropEffects.Move);
            }

            private void treeView2_DragEnter(object sender, DragEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("treeView2_DragEnter : {0}", e.Effect));
                if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", true) == true)
                {
                    e.Effect = DragDropEffects.Move;
                }
            }

            private void treeView2_DragDrop(object sender, DragEventArgs e)
            {
                TreeNode node = e.Data.GetData(typeof(TreeNode)) as TreeNode;
                if (node != null)
                {
                    if (e.Effect == DragDropEffects.Move)
                    {
                        System.Diagnostics.Debug.WriteLine(String.Format("treeView2_DragDrop : {0}", e.Effect));
                        this.treeView2.Nodes.Add((node.Clone()) as TreeNode);
                    }
                }
            }

            private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e)
            {

            }

            private void bindingSource1_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
            {
                this.treeView1.Nodes.Clear();
                for (int i = 0; i < this.bindingSource1.Count; i++)
                {
                    TreeNode node = new TreeNode((this.bindingSource1[0] as DataRowView).Row["C1"].ToString());
                    this.treeView1.Nodes.Add(node);
                }
            }

            #endregion System.Windows.Forms Drag&Drop

     

  2. Dess
    Admin
    Dess avatar
    2100 posts

    Posted 03 Jul Link to this post

    Hello Alexandre, 

    Thank you for writing.  

    Note that RadTreeView handles the whole drag and drop operation by its TreeViewDragDropService. I would recommend you to use it in order to achieve the desired drag and drop behavior. Additional information and several examples are available in the online documentation:  
    http://docs.telerik.com/devtools/winforms/treeview/drag-and-drop/modify-the-dragdropservice-behavior
    http://docs.telerik.com/devtools/winforms/treeview/drag-and-drop/drag-and-drop-in-bound-mode

    You can refer to our Demo application >> TreeView >> Drag Drop example which is also quite useful for customizing the default drag and drop behavior. The Demo application can be found in the installation folder of the suite.

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Alexandre
    Alexandre avatar
    4 posts
    Member since:
    Sep 2016

    Posted 12 Jul in reply to Dess Link to this post

    Hello Dess,

    Thank you for help.

    I used your example link and I can do drag&drop from a radtreeview to an other radtreeview or to a radlistview but I can't do it to a listview (System.Winforms) or a UserControl or a radpanel.

    How can I make it possible ?

     

    Regards,

  4. Answer
    Hristo
    Admin
    Hristo avatar
    1016 posts

    Posted 13 Jul Link to this post

    Hello Alexandre,

    Thank you for writing back.

    You can drop to standard WinForms controls using the OLE Drag and Drop support. Please check my code snippet below: 
    public partial class RadForm1 : RadForm
    {
        private RadTreeView radTreeView;
        private ListView listView;
     
        public RadForm1()
        {
            InitializeComponent();
     
            this.radTreeView = new RadTreeView
                {
                    Width = 300,
                    Dock = DockStyle.Left,
                    Parent = this
                };
     
            this.listView = new ListView
                {
                    Width = 300,
                    Dock = DockStyle.Right,
                    Parent = this
                };
     
     
            for (int i = 0; i < 10; i++)
                {
                    this.radTreeView.Nodes.Add("Node " + i);
                }
     
            for (int i = 0; i < 10; i++)
                {
                    this.listView.Items.Add("Item " + i);
                }
             
            this.listView.AllowDrop = true;
     
            this.radTreeView.MouseDown += RadTreeView_MouseDown;
            this.radTreeView.MouseMove += RadTreeView_MouseMove;
            this.listView.DragEnter += ListView_DragEnter;
            this.listView.DragDrop += ListView_DragDrop;
        }
     
        private Point mouseDownPosition;
        private bool isDragging;
        private void RadTreeView_MouseDown(object sender, MouseEventArgs e)
        {
            this.mouseDownPosition = e.Location;
        }
     
        private void RadTreeView_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                this.isDragging = false;
                return;
            }
     
            if (this.isDragging)
            {
                return;
            }
     
            if (this.ShouldBeginDrag(this.mouseDownPosition, e.Location))
            {
                TreeNodeElement draggedItem = this.radTreeView.ElementTree.GetElementAtPoint(this.mouseDownPosition) as TreeNodeElement;
                if (draggedItem != null)
                {
                    this.isDragging = true;
                    ((RadTreeView)sender).DoDragDrop(draggedItem.Data, DragDropEffects.Copy);
                }
            }
        }
     
        private void ListView_DragDrop(object sender, DragEventArgs e)
        {
            RadTreeNode node = e.Data.GetData(typeof(RadTreeNode)) as RadTreeNode;
            this.listView.Items.Add(node.Text);
        }
     
        private void ListView_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }
     
        private bool ShouldBeginDrag(Point current, Point capture)
        {
            Size dragSize = SystemInformation.DragSize;
            Rectangle dragRect = new Rectangle(capture.X - dragSize.Width / 2,
                capture.Y - dragSize.Height / 2, dragSize.Width, dragSize.Height);
            return !dragRect.Contains(current);
        }
    }

    I am also attaching a short video showing the result on my end.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  5. Alexandre
    Alexandre avatar
    4 posts
    Member since:
    Sep 2016

    Posted 13 Jul in reply to Hristo Link to this post

    Hello Hristo,

    thank you for help. This solution works for me.

Back to Top