-
Jan 21st, 2021, 03:08 PM
#1
Thread Starter
Hyperactive Member
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:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TreeView1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.TreeView1.FullRowSelect = True
Me.TreeView1.HideSelection = False
Me.TreeView1.HotTracking = True
Me.TreeView1.ShowLines = False
Me.TreeView1.DrawMode = TreeViewDrawMode.OwnerDrawText
End Sub
Private Sub treeView1_DrawNode(ByVal sender As Object, ByVal e As DrawTreeNodeEventArgs) Handles TreeView1.DrawNode
Dim font As Font = If(e.Node.NodeFont, e.Node.TreeView.Font)
Dim foreColor As Color = e.Node.ForeColor
If e.State = TreeNodeStates.Hot Then
e.Node.ForeColor = Color.Red
Else
e.Node.ForeColor = Color.Blue
End If
Dim backColor As Color = e.Node.BackColor
'If e.State = TreeNodeStates.Hot Then
' e.Node.BackColor = Color.Red
'Else
' e.Node.BackColor = Color.Blue
'End If
TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, foreColor, backColor, TextFormatFlags.GlyphOverhangPadding)
End Sub
This gives the following result:
I've previously been using a bit of code that allows the Windows OS theme to be applied to TreeViews:
vb Code:
<DllImport("uxtheme.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Function SetWindowTheme(ByVal hWnd As IntPtr, ByVal appName As String, ByVal partList As String) As Integer
End Function
SetWindowTheme(Me.TreeView1.Handle, "explorer", Nothing)
This does highlight the full width of the nodes "row" when the mouse hovers over:
I tried using the following code:
vb Code:
If e.State = TreeNodeStates.Hot Then
e.Node.BackColor = Color.Red
Else
e.Node.BackColor = Color.Blue
'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.
-
Feb 4th, 2021, 08:15 AM
#2
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.
-
Feb 4th, 2021, 08:25 AM
#3
Re: TreeView Hot Track Backcolor FullRow
Originally Posted by Poppa Mintin
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:
var t = true;
var f = false;
var a = t ? "yes" : "no"; // a will be "yes"
var b = f ? "yes" : "no"; // b will be "no"
string n = null;
string s = "Hello";
var c = n ?? "Goodbye"; // c will be "Goodbye"
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:
Dim t = True
Dim f = False
Dim a = If(t, "yes", "no") 'a will be "yes"
Dim b = If(f, "yes", "no") 'b will be "no"
Dim n As String
Dim s = "Hello"
Dim c = If(n, "Goodbye") 'c will be "Goodbye"
Dim d = If(s, "Goodbye") 'd will be "Hello"
Last edited by jmcilhinney; Feb 4th, 2021 at 08:28 AM.
-
Feb 6th, 2021, 02:09 PM
#4
Thread Starter
Hyperactive Member
Re: TreeView Hot Track Backcolor FullRow
Well you two just got my hopes right up when I saw replies to this! Dam you haha.
-
Feb 6th, 2021, 04:24 PM
#5
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:
Me.TreeView1.HotTracking = False Me.TreeView1.ShowLines = False Me.TreeView1.FullRowSelect = True Private previousNode As TreeNode = Nothing Private Sub TreeView1_NodeMouseHover(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseHoverEventArgs) Handles TreeView1.NodeMouseHover If previousNode IsNot Nothing Then previousNode.ForeColor = Nothing previousNode.BackColor = Nothing End If e.Node.ForeColor = Color.FromKnownColor(KnownColor.HighlightText) e.Node.BackColor = Color.FromKnownColor(KnownColor.Highlight) previousNode = e.Node 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)
-
Feb 7th, 2021, 05:39 PM
#6
Thread Starter
Hyperactive Member
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!
-
Feb 7th, 2021, 06:52 PM
#7
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.
-
Feb 8th, 2021, 04:17 AM
#8
Re: TreeView Hot Track Backcolor FullRow
Originally Posted by squatman
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|