xRuhRohx xRuhRohx - 5 months ago 31
Vb.net Question

HotTracking with OwnerDrawText TreeView

I am having trouble getting HotTracking to work with an OwnerDrawText TreeView.

Here is the code I have for drawing the node. I have tried using e.state and TreeNodeStates.Hot to no avail. This is the first time I have worked with an OwnerDraw object.

Private Sub tree_DrawNode(sender As Object, e As DrawTreeNodeEventArgs)
If e.Node.Level = 1 Then
HideCheckBox(e.Node)
e.DrawDefault = True
Else
TextRenderer.DrawText(e.Graphics, e.Node.Text, _
e.Node.TreeView.Font, e.Bounds, Color.Black, Color.White)
End If
End Sub

Answer

Ok so I had to set HotTracking for the TreeView to True, then here is the code I was finally able to get working.

' Draw the nodes on the TreeView
Private Sub tree_DrawNode(sender As Object, e As DrawTreeNodeEventArgs)

    If e.Node.Level = 1 Then
        HideCheckBox(e.Node)
        e.DrawDefault = True
    Else
        If (e.State And TreeNodeStates.Hot) <> 0 Then
            TextRenderer.DrawText(e.Graphics, e.Node.Text, e.Node.TreeView.Font, e.Bounds, Color.White, Color.DodgerBlue)
        Else
            TextRenderer.DrawText(e.Graphics, e.Node.Text, e.Node.TreeView.Font, e.Bounds, Color.Black, Color.White)
        End If
    End If

End Sub