Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I create a tooltip that varies by node in a TreeView?

Try using a MouseMove event handler and checking to see if you have moved to a new node, and if so, set a new tiptext.

[C#]

private int oldNodeIndex = -1;
private ToolTip toolTip1;

private void Form1_Load( object sender, EventArgs e )
{
  toolTip1 = new ToolTip();
  toolTip1.InitialDelay = 300; // half a second delay
  toolTip1.ReshowDelay = 0;
}

private void treeView1_MouseMove( object sender, MouseEventArgs e )
{
  TreeNode tn = treeView1.GetNodeAt( e.X, e.Y );
  if ( tn != null )
  {
    int currentNodeIndex = tn.Index;
    if ( currentNodeIndex != oldNodeIndex )
    {
      oldNodeIndex = currentNodeIndex;
      if ( toolTip1 != null && toolTip1.Active )
        toolTip1.Active = false; // turn it off
      toolTip1.SetToolTip( treeView1, string.Format( "Tooltip: node {0}",
        oldNodeIndex ) );
      toolTip1.Active = true; // make it active
    }
  }
}

[Visual Basic]

Private oldNodeIndex As Integer = -1
Private toolTip1 As ToolTip

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
  toolTip1 = New ToolTip
  toolTip1.InitialDelay = 300 ' half a second delay
  toolTip1.ReshowDelay = 0
End Sub

Private Sub treeView1_MouseMove(ByVal sender As Object, _
    ByVal e As MouseEventArgs)
  Dim tn As TreeNode = treeView1.GetNodeAt(e.X, e.Y)
  If Not (tn Is Nothing) Then
    Dim currentNodeIndex As Integer = tn.Index
    If currentNodeIndex <> oldNodeIndex Then
      oldNodeIndex = currentNodeIndex
      If Not (toolTip1 Is Nothing) And toolTip1.Active Then
        toolTip1.Active = False ' turn it off
      End If
      toolTip1.SetToolTip(treeView1, String.Format("Tooltip: node {0}", _
        oldNodeIndex))
      toolTip1.Active = True ' make it active so it can show
    End If
  End If
End Sub

Contributed from George Shepherd's Windows Forms FAQ



Page view counter