i wanna draw line refers to the node in treeview which the mouse hits in dragging to give the user the ability to see the location that the he would drop the node in something like the drag and drop in windows like this image
-
Can you show the code you have so far, i.e. the d&d code? – TaW Mar 21 '19 at 15:39
1 Answers
Afaik no events are generated during the moving part of a drag-drop operation.
To create feedback you can use a Timer
; start it in the ItemDrag
event and stop in in the DragDrop
.
In the Tick
you could provide visual feedback by maybe selecting the node currently under the mouse cursor or by drawing a line.
Here is how you get the node under the cursor and draw a line:
private void timer1_Tick(object sender, EventArgs e)
{
if (Control.MouseButtons.HasFlag(MouseButtons.Left))
{
using (Graphics g = treeView1.CreateGraphics())
{
treeView1.Refresh();
var hitt = treeView1.HitTest(treeView1.PointToClient(Control.MousePosition));
var n = hitt.Node;
if (n != null)
{
int y = n.Bounds.Y; // draw above the node; maybe change to n.Bound.Bottom ?
Size sz = treeView1.ClientSize;
g.DrawLine(Pens.Orange, 0, y, sz.Width, y);
}
}
}
}
Note that is one of the rare cases where you use control.CreateGraphics()
to actually draw onto a control.
Also note that often the tricky part is really to decide on where the drop should go: Onto the same level as the node (thus reordering the items on the same level) or one level closer to the root as the last node before..? - Example: Windows File Explorer will not let you reorder items.

- 53,122
- 8
- 69
- 111
-
Do u have any solution to the flickering because "treeView1.Refresh();" each time ? – nesma mostafa Mar 27 '19 at 06:38
-
My tree isn't so large and I don't see any flicker. You might try to add [DoubleBuffering](https://stackoverflow.com/questions/44185298/update-datagridview-very-frequently/44188565#44188565) but I'm not sure if that helps.. – TaW Mar 27 '19 at 08:39
-
I bet this could be improved by using the BeginUpdate / EndUpdate on treeview – Learner Feb 27 '21 at 12:34
-