Link to home
Start Free TrialLog in
Avatar of DreamU
DreamU

asked on

TreeView HideSelection False removes highlight

I have a TreeView control and when the user clicks on a node it highlights fine. When the focus moves away from the control that highlight disappears. So,

categoryTree.HideSelection = False

should fix it, right? No luck, highlight disappears as soon as control loses focus.

So, I decide to set my own background color on the Leave event:

categoryTree.SelectedNode.BackColor = Color.LightBlue
categoryTree.Invalidate()
categoryTree.Refresh()

This does NOT change the node's Backcolor till the control receives the focus again! Huh?

So, I´m thinking maybe there is some system code that executes on the control after my Leave event or the Refresh just doesn´t execute. As a last attempt I put that same code in the next control which receives focus (not a solution just a test). The system still does not repaint the TreeView control with that node´s background color. I've never seen Windows not repaint after the Refresh().

I am striking out 3 for 3 on this feature. I´d try Drawmode = OwnerDrawText but since the control doesn't appear to refresh without the focus I am leery about doing all that extra work for nothing. I am new to using this control - am I just missing something simple?

Avatar of Dana Seaman
Dana Seaman
Flag of Brazil image

This code works for Vb6 Treeview. Code should be similar for Vb Express 2008.
If you Select a node programatically you should also call "SetSelectedBkgd".
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
   SetSelectedBkgd
End Sub
 
Private Sub SetSelectedBkgd()
   Dim nod              As Node
   For Each nod In TreeView1.Nodes
      If nod.Selected Then 
         nod.BackColor = &HFF9933
         nod.ForeColor = vbWhite
      Else
         nod.BackColor = vbWhite
         nod.ForeColor = vbBlack
      End If
   Next
End Sub

Open in new window

Avatar of DreamU
DreamU

ASKER

Thanks for responding danaseaman. I tried your code out - only a few minor changes for VB Express 2008. It solves a future problem I knew I was going to have in terms of having to know which node to turn off later - answer was: don´t need to remember, great.

Unfortunately, the code did not highlight the node. That line that says nod.backcolor = Color.red was executed once, as expected, but the node did not turn red. As before, I called the routine on Select, then on Leave, then when the focus arrives in another contol. This is not unexpected as you see in my original code I tried turning the node LightBlue and that never worked either.

So, I added a little logic to turn the node just before the selected node red. THAT WORKED! It appears you can color any node EXCEPT the one that is currently highlighted. PITY.
Private Sub SetCatHighlight()
        Dim nod As TreeNode
        For Each nod In CatTree.Nodes
            If nod.IsSelected Then
                nod.BackColor = Color.Red
                nod.ForeColor = Color.White
            Else
                nod.BackColor = Color.White
                nod.ForeColor = Color.Black
            End If
        Next
    End Sub

Open in new window

SOLUTION
Avatar of Dana Seaman
Dana Seaman
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DreamU

ASKER

It appears that MS must have rewritten some of the treeview code for VB 2008. Unless someone with 2008 confirms they successfully changed the color of the selected row or that HideSelection=False works I am going to consider this a bug in control code.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial