Winforms TreeView, I want to scroll up/down when the user drag and item.
Pretty much the same as the above, but without that 'top' bug and a bit simpler to use in bigger projects.
Add this class to your project:
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);
}
}
}
Then simply subscribe to the DragOver event of your treeview (or any other control/custom control you want scrolling enabled while drag/dropping) and call the Scroll() method.
private void treeView_DragOver(object sender, DragEventArgs e)
{
treeView.Scroll();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With