How to perform the node drag drop between an XPToolBar and TreeViewAdv?
(Views :1095)

Please refer the following steps to be performed to achieve this:

  • Place the TreeViewAdv and XPToolBar on the form.
  • Add nodes to the TreeViewAdv.
  • Set the AllowDrop property as True of the XPToolBar.
  • Handle the TreeViewAdv MouseDown event to access the node which is going to drag.
  • Handle the DragDrop and DragEnter events of XPToolBar to get the which is dragged from the TreeViewAdv.
  • Please refer the below code snippet which ilustrates this:

    C#
    private void xpToolBar1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
    {
    Console.WriteLine("XPToolBar Drag Enter");
    if (e.Data.GetDataPresent(typeof(TreeNodeAdv)))
    e.Effect = DragDropEffects.Copy;
    else
    e.Effect = DragDropEffects.None;
    }
    private void xpToolBar1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {
    Console.WriteLine("XPToolBar Drag Drop");
    if (e.Data.GetDataPresent(typeof(TreeNodeAdv)))
    {
    TreeNodeAdv node = e.Data.GetData(typeof(TreeNodeAdv)) as TreeNodeAdv;
    BarItem bitem = new BarItem(node.Text);
    this.xpToolBar1.Items.Add(bitem);
    }
    }
    private void treeViewAdv1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if (e.Button == MouseButtons.Left)
    {
    // get the point where the mouse was left clicked
    Point p = this.treeViewAdv1.PointToClient(Control.MousePosition);
    // get the node at the left click point
    TreeNodeAdv node = this.treeViewAdv1.PointToNode(p);
    this.treeViewAdv1.SelectedNode = node;
    this.treeViewAdv1.Refresh();
    DoDragDrop(node, DragDropEffects.Copy);
    }
    }
    VB
    Private Sub XpToolBar1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles XpToolBar1.DragDrop
    Console.WriteLine("XPToolBar Drag Drop")
    If e.Data.GetDataPresent(GetType(TreeNodeAdv)) Then
    Dim node As TreeNodeAdv = CType(IIf(TypeOf e.Data.GetData(GetType(TreeNodeAdv)) Is TreeNodeAdv, e.Data.GetData(GetType(TreeNodeAdv)), Nothing), TreeNodeAdv)
    Dim bitem As BarItem = New BarItem(node.Text)
    Me.XpToolBar1.Items.Add(bitem)
    End If
    End Sub
    Private Sub XpToolBar1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles XpToolBar1.DragEnter
    Console.WriteLine("XPToolBar Drag Enter")
    If e.Data.GetDataPresent(GetType(TreeNodeAdv)) Then
    e.Effect = DragDropEffects.Copy
    Else
    e.Effect = DragDropEffects.None
    End If
    End Sub
    Private Sub TreeViewAdv1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeViewAdv1.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Left Then
    ' get the point where the mouse was left clicked
    Dim p As Point = Me.TreeViewAdv1.PointToClient(Control.MousePosition)
    ' get the node at the left click point
    Dim node As TreeNodeAdv = Me.TreeViewAdv1.PointToNode(p)
    Me.TreeViewAdv1.SelectedNode = node
    Me.TreeViewAdv1.Refresh()
    DoDragDrop(node, DragDropEffects.Copy)
    End If
    End Sub

    Sample:

    http://websamples.syncfusion.com/samples/KB/Tools.Windows/TDragDrop/main.htm
    ::adCenter::