Microsoft KB Archive/319963

= How to add a ToolTip to a TreeNode in Visual Basic 2005 or in Visual Basic .NET =

Article ID: 319963

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework Class Libraries 1.1
 * Microsoft .NET Framework Class Libraries 1.0

-



This article was previously published under Q319963





For a Microsoft Visual C# version of this article, see 322634.



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 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 you need:
 * Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * Visual Basic 2005 syntax or Visual Basic .NET syntax
 * Microsoft Windows Forms

Create and then populate the sample form
 Start Visual Studio 2005 or Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates.

Note In Visual Studio 2005, click Visual Basic under Project Types. Add a TreeView control to Form1.

Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

 Add a ToolTip control to Form1.

Add the ToolTip to the TreeNodes
 Right-click Form1, and then click View Code.</li> In the Class Name box, click (Base Class Events).</li> In the Method Name box, click Load.</li>  Add the following code to the Load event. Dim count As Integer Dim rootNode As TreeNode Dim childNode As TreeNode Dim day As DayOfWeek

' Create a root node. rootNode = TreeView1.Nodes.Add(&quot;Day of Week&quot;)

' Create a series of child nodes, and then set the Tag property for each child node. For count = 0 To 6 day = CType(count, DayOfWeek) childNode = rootNode.Nodes.Add(day.ToString) childNode.Tag = &quot;This day is &quot; & day.ToString & &quot;.&quot; Next

' Expand all TreeView nodes. rootNode.ExpandAll </li> In the Class Name box, click TreeView1.</li> In the Method Name box, click MouseMove.</li>  Add the following code to the MouseMove event of the TreeView control. ' Determine the node that is currently at the mouse pointer location. Dim theNode As TreeNode = TreeView1.GetNodeAt(e.X, e.Y)

' Check if mouse is paused over an actual node. If Not (theNode Is Nothing) Then ' Only update the ToolTip if tip needs to be changed. If (theNode.Tag <> ToolTip1.GetToolTip(TreeView1)) Then ToolTip1.SetToolTip(TreeView1, theNode.Tag) End If       Else ' Mouse is not paused over a node. Therefore, clear the ToolTip. ToolTip1.SetToolTip(TreeView1, &quot;&quot;) End If                   </li> Save and then run the program. If you move the mouse pointer and pause over one of the nodes, a ToolTip appears.</li></ol>

<div class="references_section">