How to add a ToolTip to a TreeNode in Visual C# (322634)



The information in this article applies to:

  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2002)
  • Microsoft .NET Framework Class Libraries 1.0

This article was previously published under Q322634
For a Microsoft Visual Basic .NET version of this article, see 319963.

IN THIS TASK

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.

back to the top

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
back to the top

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.
back to the top

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:
  3. Save and then run the program. If you pause the mouse pointer on one of the nodes, a ToolTip appears.
back to the top

REFERENCES

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

Modification Type:MajorLast Reviewed:1/19/2006
Keywords:kbCtrl kbHOWTOmaster KB322634 kbAudDeveloper