Microsoft KB Archive/322634

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

Article ID: 322634

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * 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
 * Requirements
 * Create and Populate the Sample Form
 * Add the ToolTip to the TreeNodes

REFERENCES



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
  Paste the following code into the Form1 Load event: // Create a root node. TreeNode rootNode = treeView1.Nodes.Add(&quot;Day of Week&quot;);

// 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 = &quot;This day is &quot; + day.ToString + &quot;.&quot;; }

// Expand all of the TreeView nodes. rootNode.ExpandAll;   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 &quot;null&quot;. 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, &quot;&quot;); } } else    // Pointer is not over a node so clear the ToolTip. {  this.toolTip1.SetToolTip(this.treeView1, &quot;&quot;); } 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

 Save and then run the program. If you pause the mouse pointer on one of the nodes, a ToolTip appears.

back to the top

