Display tooltips for TreeView nodes in C#


At design time, I added a TreeView control to the form. I also added an associated ImageList control to hold images for the TreeView control’s nodes, and I set the TreeView control’s ImageList property to the ImageList control. Finally I added a ToolTip component to the form and named it ttOrg.

When the program starts, it makes three types of TreeView nodes representing factories, groups, and persons. It makes an associated object for each node and assigns it to the node’s Tag property to give the node’s extra information about the objects they represent.

In this example the object classes FactoryData, GroupData, and PersonData really just hold names to display in the tooltips, but in a real program you could give them more interesting information and methods.

The following code shows how the program makes a new factory node.

factory = AddTreeViewNode(trvOrg.Nodes,
     "R & D", imFactory,
    new FactoryData("Factory: R & D"));

Here the parameters to the AddTreeViewNode method represent:

  • The TreeView control’s Nodes collection
  • The text to display on the node
  • A number giving the index of the node’s picture in the ImageList control
  • The object that should be stored in the new node’s Tag property

The following code shows the AddTreeViewNode method that the previous code invokes.

// Add a new node to the collection.
private TreeNode AddTreeViewNode(TreeNodeCollection parent_nodes,
    string text, int image_index, object tag_object)
    TreeNode new_node = parent_nodes.Add(text);
    new_node.ImageIndex = image_index;
    new_node.SelectedImageIndex = image_index;
    new_node.Tag = tag_object;
    return new_node;

The method adds a new TreeNode to the TreeNodeCollection passed in as the first parameter. It sets the node’s ImageIndex to indicate which ImageList image should be displayed for this node. Finally it sets the node’s Tag property to the associated object.

The following code shows the simple FactoryData class used by this example.

public class FactoryData
    public string Name = "";

    // Initializing constructor.
    public FactoryData(string new_name)
        Name = new_name;

While the program is running, the TreeView control’s trvOrg_MouseMove event handler shown in the following code displays the tooltips.

// Display the appropriate tooltip.
private TreeNode old_node = null;
private void trvOrg_MouseMove(object sender, MouseEventArgs e)
    // Find the node under the mouse.
    TreeNode node_here = trvOrg.GetNodeAt(e.X, e.Y);
    if (node_here == old_node) return;
    old_node = node_here;

    // See if we have a node.
    if (old_node == null)
        ttOrg.SetToolTip(trvOrg, "");
        // Get this node's object data.
        if (node_here.Tag is FactoryData)
            FactoryData factory_data = node_here.Tag as FactoryData;
            ttOrg.SetToolTip(trvOrg, factory_data.Name);
        else if (node_here.Tag is GroupData)
            GroupData group_data = node_here.Tag as GroupData;
            ttOrg.SetToolTip(trvOrg, group_data.Name);
        else if (node_here.Tag is PersonData)
            PersonData person_data = node_here.Tag as PersonData;
            ttOrg.SetToolTip(trvOrg, person_data.Name);

This event handler uses the TreeView control’s GetNodeAt method to see which node is under the mouse. If this is the same as the previously found node, the event handler returns.

If the new node is null, the code sets the TreeView control’s ToolTip to an empty string to remove the previous tooltip.

If the new node is not null, the code examines the node’s type and sets the ToolTip appropriately.

The key is the GetNodeAt method, which you can use to find the node under the mouse.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in controls, user interface and tagged , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to Display tooltips for TreeView nodes in C#

  1. Pingback: Display context menus for TreeView items in C#

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.