Tree Node Font or graphics change On Mouse HOver

I want To change the font or graphical display of nodes in a treeview on mouse hover.It means that whenevr user hovers the mouse over each node the node has a different effect and then changes back to its previous font when the mouse shifts to another node and so on.
Thanx in advance.
Amitava_MukherjeeAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
If its a WinForms app then you need to keep a reference to the previously hovered over node, seeing as there is not event that allows for a node losing hover:
    Private Sub TreeView1_NodeMouseHover(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseHoverEventArgs) Handles TreeView1.NodeMouseHover

        '// Check if there is a previous node. Set its font normal if there is.
        If Not previousNode Is Nothing AndAlso Not previousNode.Equals(e.Node) Then
            previousNode.NodeFont = New Font(TreeView1.Font, FontStyle.Regular)
        End If

        '// Set a bold font on the current hover node
        e.Node.NodeFont = New Font(TreeView1.Font, FontStyle.Bold)
        previousNode = e.Node

    End Sub

    Dim previousNode As TreeNode = Nothing

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Is this in a Web or Windows application?
0
 
Meir RivkinFull stack Software EngineerCommented:
  1.  Open Visual Studio.NET 2005 editor and create a new C# Windows project
   2. Open Form1 in designer mode
   3. Drag and drop a TreeView control on the Form1
   4. Add few nodes to the TreeView control(use the property Nodes to add nodes)
   5. Set the property DrawNode of the TreeView control to OwnerDrawText
6. Set the property HotTracking to true
  7. Add the following code to DrawNode event of the TreeView control and execute the project:

private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            if ((e.State & TreeNodeStates.Hot) != 0)
            {
                Font f = new Font("Arial", 10);
                e.Graphics.DrawString(e.Node.Text, f, Brushes.Red, e.Bounds);
            }
            else
            {
                e.DrawDefault = true;
            }
        }

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Meir RivkinFull stack Software EngineerCommented:
sorry, u need vb.net:
Private Sub treeView1_DrawNode(ByVal sender As Object, ByVal e As DrawTreeNodeEventArgs)
    If (e.State And TreeNodeStates.Hot) <> 0 Then
        Dim f As New Font("Arial", 10)
        e.Graphics.DrawString(e.Node.Text, f, Brushes.Red, e.Bounds)
    Else
        e.DrawDefault = True
    End If
End Sub

Open in new window

0
 
Amitava_MukherjeeAuthor Commented:
Thanx For Your help
@sedgwick: In my application i am generating the nodes programatically.Strangely the event u suggested never fires in my application.

@carl_tawn: Ur code was something i was looking for...i kind of modified it to fit into my application.Actually i hav nodes of diffrenet fonts and font styles in my treeview.Here goes my code.:


Private Sub tvLeft_NodeMouseHover(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseHoverEventArgs) Handles tvLeft.NodeMouseHover
                    If Not ctnPast Is Nothing AndAlso Not ctnPast.Equals(e.Node) Then
                ctnPast.NodeFont = New Font(ctnPast.NodeFont.OriginalFontName, ctnPast.NodeFont.Size, sOldStyle)
            End If

             sOldStyle = e.Node.NodeFont.Style
            ctnPast = e.Node

            e.Node.NodeFont = New Font(e.Node.NodeFont.OriginalFontName, e.Node.NodeFont.Size, FontStyle.Bold)
      

    End Sub

Dim sOldStyle As New FontStyle
Private ctnPast As TreeNode = Nothing

Open in new window

0
 
Amitava_MukherjeeAuthor Commented:
Since my purpose  is served am closing the question.thanx again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.