What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleChange a TreeView control's tooltip when the mouse moves over different nodes in VB .NET
DescriptionThis example shows how to change a TreeView control's tooltip when the mouse moves over different nodes in Visual Basic .NET.
KeywordsTreeView, tooltip, TreeView tooltip, ToolTipText
CategoriesControls, VB.NET
This program makes three types of TreeView nodes representing factories, groups, and persons. It sets each makes an associated object for each node and assigns it to the node's Tag property. It makes a FactoryData object for a factory node, a GroupData object for a group node, and a PersonData object for a person node. These objects don't do much in this example but you can give them whatever data you want.

The trvOrg_MouseMove event handler uses the TreeView control's GetNodeAt method to see which node is under the mouse. If this is different from the previous node, the routine checks the type of the node's Tag object. It then sets the TreeView control's Tooltip to that object's Name property.

Note that a control doesn't have its own Tooltip property in Visual Basic .NET. Instead a related ToolTip control provides tooltips for various controls. This code uses the ToolTip control's SetToolTip method to set the tooltip for the TreeView.

Private Sub trvOrg_MouseMove(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.MouseEventArgs) Handles _
    ' Find the node under the mouse.
    Static old_node As TreeNode
    Dim node_here As TreeNode = trvOrg.GetNodeAt(e.X, e.Y)
    If node_here Is old_node Then Exit Sub
    old_node = node_here

    ' See if we have a node.
    If old_node Is Nothing Then
        ttOrg.SetToolTip(trvOrg, "")
        ' Get this node's object data.
        If TypeOf node_here.Tag Is FactoryData Then
            Dim factory_data As FactoryData = _
                DirectCast(node_here.Tag, FactoryData)
            ttOrg.SetToolTip(trvOrg, factory_data.Name)
        ElseIf TypeOf node_here.Tag Is GroupData Then
            Dim group_data As GroupData = _
                DirectCast(node_here.Tag, GroupData)
            ttOrg.SetToolTip(trvOrg, group_data.Name)
        ElseIf TypeOf node_here.Tag Is PersonData Then
            Dim person_data As PersonData = _
                DirectCast(node_here.Tag, PersonData)
            ttOrg.SetToolTip(trvOrg, person_data.Name)
        End If
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.