Results 1 to 8 of 8

Thread: TreeView Hot Track Backcolor FullRow

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2012
    Posts
    304

    Question TreeView Hot Track Backcolor FullRow

    Hi there,

    I've been trying to customise the background colour of a TreeView node when the mouse hovers over it. I've been looking into the HotTracking property for this, and I also want it to fill the full width of the nodes "row".

    With the following code I've managed to customise the colour of the node's text whilst the mouse is hovered (HotTracked).

    vb Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.  
    3.         Me.TreeView1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    4.         Me.TreeView1.FullRowSelect = True
    5.         Me.TreeView1.HideSelection = False
    6.         Me.TreeView1.HotTracking = True
    7.         Me.TreeView1.ShowLines = False
    8.  
    9.         Me.TreeView1.DrawMode = TreeViewDrawMode.OwnerDrawText
    10.     End Sub
    11.  
    12.     Private Sub treeView1_DrawNode(ByVal sender As Object, ByVal e As DrawTreeNodeEventArgs) Handles TreeView1.DrawNode
    13.         Dim font As Font = If(e.Node.NodeFont, e.Node.TreeView.Font)
    14.         Dim foreColor As Color = e.Node.ForeColor
    15.  
    16.         If e.State = TreeNodeStates.Hot Then
    17.             e.Node.ForeColor = Color.Red
    18.         Else
    19.             e.Node.ForeColor = Color.Blue
    20.         End If
    21.  
    22.         Dim backColor As Color = e.Node.BackColor
    23.  
    24.         'If e.State = TreeNodeStates.Hot Then
    25.         '    e.Node.BackColor = Color.Red
    26.         'Else
    27.         '    e.Node.BackColor = Color.Blue
    28.         'End If
    29.  
    30.         TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, foreColor, backColor, TextFormatFlags.GlyphOverhangPadding)
    31.     End Sub

    This gives the following result:

    Name:  tree1.png
Views: 877
Size:  4.8 KB


    I've previously been using a bit of code that allows the Windows OS theme to be applied to TreeViews:

    vb Code:
    1. <DllImport("uxtheme.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
    2.     Public Function SetWindowTheme(ByVal hWnd As IntPtr, ByVal appName As String, ByVal partList As String) As Integer
    3.     End Function
    4.  
    5. SetWindowTheme(Me.TreeView1.Handle, "explorer", Nothing)

    This does highlight the full width of the nodes "row" when the mouse hovers over:

    Name:  tree2.png
Views: 832
Size:  4.5 KB

    I tried using the following code:

    vb Code:
    1. If e.State = TreeNodeStates.Hot Then
    2.             e.Node.BackColor = Color.Red
    3.         Else
    4.             e.Node.BackColor = Color.Blue
    5.         'End If

    But this just changes the area behind the width of the node's text, not it's full row width.

    Can anyone suggest how I can target the full row widths colour please. Do I need draw a rectangle or something to the size I need?

    Any help with this would be greatly appreciated, thanks in advance.

  2. #2
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: TreeView Hot Track Backcolor FullRow

    First, I'm surprised that your question hasn't been answered before now, such a long delay is most unusual in this forum.

    Sadly I don't have an answer, I do have a query.
    In your first code snippet, line 13: I wonder why you don't get an error 'If' without a 'Then' ?


    Poppa

    (At least this will bring the question back to the fore)
    Along with the sunshine there has to be a little rain sometime.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: TreeView Hot Track Backcolor FullRow

    Quote Originally Posted by Poppa Mintin View Post
    In your first code snippet, line 13: I wonder why you don't get an error 'If' without a 'Then' ?
    That's because it's an If operator, not an If statement. C# has the ternary operator and the null-coalescing operator:
    csharp Code:
    1. var t = true;
    2. var f = false;
    3.  
    4. var a = t ? "yes" : "no"; // a will be "yes"
    5. var b = f ? "yes" : "no"; // b will be "no"
    6.  
    7. string n = null;
    8. string s = "Hello";
    9.  
    10. var c = n ?? "Goodbye"; // c will be "Goodbye"
    11. var d = s ?? "Goodbye"; // d will be "Hello"
    VB has If operators that do the same things. If with three operands is equivalent to the ternary operator (a better version of the IIf function) and If with two operands, as in post #1, is equivalent to the null coalescing operator. Basically, return the first operand if it is not Nothing, otherwise return the second operand.
    vb.net Code:
    1. Dim t = True
    2. Dim f = False
    3.  
    4. Dim a = If(t, "yes", "no") 'a will be "yes"
    5. Dim b = If(f, "yes", "no") 'b will be "no"
    6.  
    7. Dim n As String
    8. Dim s = "Hello"
    9.  
    10. Dim c = If(n, "Goodbye") 'c will be "Goodbye"
    11. Dim d = If(s, "Goodbye") 'd will be "Hello"

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2012
    Posts
    304

    Re: TreeView Hot Track Backcolor FullRow

    Well you two just got my hopes right up when I saw replies to this! Dam you haha.

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: TreeView Hot Track Backcolor FullRow

    from here :http://vbcity.com/forums/t/164681.aspx

    This should give what you want (tested on VB 2010)

    VB.net Code:
    1. Me.TreeView1.HotTracking = False
    2. Me.TreeView1.ShowLines = False
    3. Me.TreeView1.FullRowSelect = True
    4.  
    5. Private previousNode As TreeNode = Nothing
    6.  
    7. Private Sub TreeView1_NodeMouseHover(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseHoverEventArgs) Handles TreeView1.NodeMouseHover
    8.         If previousNode IsNot Nothing Then
    9.             previousNode.ForeColor = Nothing
    10.             previousNode.BackColor = Nothing
    11.         End If
    12.  
    13.         e.Node.ForeColor = Color.FromKnownColor(KnownColor.HighlightText)
    14.         e.Node.BackColor = Color.FromKnownColor(KnownColor.Highlight)
    15.  
    16.         previousNode = e.Node
    17. End Sub
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2012
    Posts
    304

    Re: TreeView Hot Track Backcolor FullRow

    Delaney,

    Thank you for your reply I really appreciate it.

    Unfortunately this only sort of works, it only changes colour when you mouseover the actual text not the nodes full row width. Also it is strangely quite slow to change the colour, unlike "HotTracking" which is instant.

    The search continues!

  7. #7
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: TreeView Hot Track Backcolor FullRow

    Thanks John,

    That's very enlightening, something else for my collection of snippets.


    Poppa


    I just discovered that I didn't send this post !

    Pop.
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: TreeView Hot Track Backcolor FullRow

    Quote Originally Posted by squatman View Post
    Delaney,

    Thank you for your reply I really appreciate it.

    Unfortunately this only sort of works, it only changes colour when you mouseover the actual text not the nodes full row width. Also it is strangely quite slow to change the colour, unlike "HotTracking" which is instant.

    The search continues!
    EDIT : rereading your post make me discover I didn't fully understand it the first time (I thought that for you it didn't highlight the full row). So I discard my previous answer.
    Last edited by Delaney; Feb 8th, 2021 at 04:23 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width