How to add a ToolTip to a TreeNode in Visual C#

Article translations Article translations
Article ID: 322634 - View products that this article applies to.
This article was previously published under Q322634
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article describes how to add a ToolTip to the nodes of a TreeView control. The ToolTip displays information about the TreeNode over which the mouse pointer is paused. Although the TreeView control does not have a ToolTip property, you can use the ToolTip control to provide ToolTip functionality.

The example that is described in this article demonstrates this by using a TreeView control that displays the days of the week. When the mouse pointer pauses over one of the TreeNodes, a ToolTip that indicates the day of the week appears.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
This article assumes that you are familiar with the following topics:
  • Visual C# syntax
  • Windows forms

Create and Populate the Sample Form

  1. Create a new Windows program in Visual C#.
  2. Add a TreeView control to Form1.
  3. Add a ToolTip control to Form1.

Add the ToolTip to the TreeNodes

  1. Paste the following code into the Form1 Load event:
    // Create a root node.
    TreeNode rootNode = treeView1.Nodes.Add("Day of Week");
    
    // Create a series of child nodes and then set the Tag property for each.
    for (int count = 0; count <= 6; count++)
    {
       DayOfWeek day = (DayOfWeek)count;
       TreeNode childNode = rootNode.Nodes.Add(day.ToString());
       childNode.Tag = "This day is " + day.ToString() + ".";
    }
    
    // Expand all of the TreeView nodes.
    rootNode.ExpandAll();
    					
  2. Paste the following code into the TreeView MouseMove event:
    // Get the node at the current mouse pointer location.
    TreeNode theNode =  this.treeView1.GetNodeAt(e.X, e.Y);
    
    // Set a ToolTip only if the mouse pointer is actually paused on a node.
    if ((theNode != null))
    {
       // Verify that the tag property is not "null".
       if (theNode.Tag != null)
       {
          // Change the ToolTip only if the pointer moved to a new node.
          if (theNode.Tag.ToString()!=this.toolTip1.GetToolTip(this.treeView1))
          {
             this.toolTip1.SetToolTip(this.treeView1, theNode.Tag.ToString());
          }
       }     
       else
       {
          this.toolTip1.SetToolTip(this.treeView1, "");
       }
    }
    else     // Pointer is not over a node so clear the ToolTip.
    {
       this.toolTip1.SetToolTip(this.treeView1, "");
    }
    					
    Note The code should be changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# adds one form to the project by default. This form is named Form1. The two files that represent the form are named Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by dragging and dropping controls from the Toolbox. For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site:
    http://msdn2.microsoft.com/en-us/library/ms173077.aspx
  3. Save and then run the program. If you pause the mouse pointer on one of the nodes, a ToolTip appears.

REFERENCES

For additional information, visit the following Microsoft Web site to see the Microsoft .NET Framework Software Development Kit (SDK) documentation:
TreeView Class

Properties

Article ID: 322634 - Last Review: December 11, 2006 - Revision: 3.4
APPLIES TO
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft .NET Framework Class Libraries 1.0
Keywords: 
kbctrl kbhowtomaster KB322634

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com