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.
- Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
- Visual C# syntax
- Windows forms
- Create a new Windows program in Visual C#.
- Add a TreeView control to Form1.
- Add a ToolTip control to Form1.
- 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.