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.
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
- Create a new Windows program in Visual C#.
- Add a TreeView control to Form1.
- Add a ToolTip control to Form1.
Add the ToolTip to the TreeNodes
- 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.
- Paste the following code into the TreeView MouseMove event:
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:
// 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.
else // Pointer is not over a node so clear the ToolTip.
- Save and then run the program. If you pause the mouse pointer on one of the nodes, a ToolTip appears.
For additional information, visit the following Microsoft Web site to see the Microsoft .NET Framework Software Development Kit (SDK) documentation:
Article ID: 322634 - Last Review: December 11, 2006 - Revision: 3.4
- Microsoft Visual C# 2005 Express Edition
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft .NET Framework Class Libraries 1.0
|kbctrl kbhowtomaster KB322634|