Question Custom Cursor during Drag into a TreeView

curtrude

New member
Joined
Sep 8, 2008
Messages
2
Programming Experience
Beginner
I have found several sources that suggest using the GiveFeedback Event to be able to do custom cursors while dragging into a component. I have successfully used this approach with ListBoxes, but it doesn't work with a TreeView. The GiveFeedback Event seems not to be fired for the TreeView. Any suggestions.

Thanks,
Curt

VB.NET:
    Private Sub TreeViewSections_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeViewSections.DragEnter

        If e.Data.GetDataPresent(DataFormats.Text) Then
            e.Effect = DragDropEffects.Copy
            Dim temp As String = "asadd"

            drawcursor(CStr(e.Data.GetData(temp.GetType)), TreeViewSections.Font)

        Else
            e.Effect = DragDropEffects.None

        End If

    End Sub

    Private Sub TreeViewSections_GiveFeedback(ByVal sender As Object, ByVal e As GiveFeedbackEventArgs) Handles TreeViewSections.GiveFeedback

        e.UseDefaultCursors = False
        Windows.Forms.Cursor.Current = textcursor

    End Sub

    Sub drawcursor(ByVal text As String, ByVal fnt As Font)
        Dim bmp As New Bitmap(1, 1)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim sz As SizeF = g.MeasureString(text, fnt)
        bmp = New Bitmap(CInt(sz.Width), CInt(sz.Height))
        g = Graphics.FromImage(bmp)
        g.Clear(Color.White)
        g.DrawString(text, fnt, Brushes.Black, 0, 0)
        g.Dispose()
        textcursor = New Cursor(bmp.GetHicon)
        bmp.Dispose()
    End Sub

    Dim textcursor As Cursor
 
I have found several sources that suggest using the GiveFeedback Event to be able to do custom cursors while dragging into a component. I have successfully used this approach with ListBoxes, but it doesn't work with a TreeView. The GiveFeedback Event seems not to be fired for the TreeView. Any suggestions.
It could be a bug, or not implemented for this control, documentations says nothing about this. I see the same, and see some forum questions about this, but no answers. I also tried the "mousedown-approach" in case the special ItemDrag was too late or something in relatation to this drag event, but same result.
 
I was able to get the GiveFeedback event to fire by using the following.

VB.NET:
	Public Sub TreeView_ItemDrag(ByVal sender As Object, ByVal e As ItemDragEventArgs) _
	Handles TreeView1.ItemDrag, TreeView2.ItemDrag

		sender.DoDragDrop(e.Item, DragDropEffects.Move)

	End Sub

VB.NET:
	Private Sub TreeView1_GiveFeedback(ByVal sender As System.Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) _
	Handles TreeView1.GiveFeedback, TreeView2.GiveFeedback

		If e.Effect = DragDropEffects.Move Then
			e.UseDefaultCursors = False
			Me.TreeView1.Cursor = New Cursor("blah.ico")
		Else
			e.UseDefaultCursors = True
		End If

	End Sub
 
I was doing something stupid like calling theForm.DoDragDrop instead of theTreeview.DoDragDrop (quickly misled by the article MattP linked :)), now GiveFeedback is back and giving for me too.
 

Latest posts

Back
Top