Hi Sudip Bhatt,
First, I use the following code to drag and drop the node what you have achieved.
private void Form1_Load(object sender, EventArgs e)
{
this.treeView1.ItemDrag += new ItemDragEventHandler(this.treeView1_ItemDrag);
this.treeView1.DragDrop += new DragEventHandler(this.treeView1_DragDrop);
this.treeView1.DragEnter += new DragEventHandler(this.treeView1_DragEnter);
}
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
var _selectedNode = (TreeNode)e.Item;
}
private void treeView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
// Retrieve the client coordinates of the drop location.
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));
// Retrieve the node at the drop location.
TreeNode targetNode = treeView1.GetNodeAt(targetPoint);
// Retrieve the node that was dragged.
TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
// Confirm that the node at the drop location is not
// the dragged node and that target node isn't null
// (for example if you drag outside the control)
if (!draggedNode.Equals(targetNode) && targetNode != null)
{
// Remove the node from its current
// location and add it to the node at the drop location.
draggedNode.Remove();
targetNode.Nodes.Add(draggedNode);
// Expand the node at the location
// to show the dropped node.
targetNode.Expand();
}
}
Then in order to tell the treeview to scroll up or down, you need to call the Windows API SendMessage() function.
And next, determine where the mouse cursor is in relation to the top and bottom of the treeview control in the DragScroll event. Then call SendMessage to scroll as apporpriate.
Here is a code example you can refer to.
public static class NativeMethods
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
public static void Scroll(this Control control)
{
var pt = control.PointToClient(Cursor.Position);
if ((pt.Y + 20) > control.Height)
{
// scroll down
SendMessage(control.Handle, 277, (IntPtr)1, (IntPtr)0);
}
else if (pt.Y < 20)
{
// scroll up
SendMessage(control.Handle, 277, (IntPtr)0, (IntPtr)0);
}
}
}
The test result:
Best Regards,
Daniel Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.