Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

24. Windows Forms TreeView

24.6 How can I get a tooltip to vary from node to node in my 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, System.EventArgs e)
     {
          this.toolTip1 = new System.Windows.Forms.ToolTip();
          this.toolTip1.InitialDelay = 300; //half a second delay
          this.toolTip1.ReshowDelay = 0;
     }
     
     private void treeView1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
     {
          TreeNode tn = this.treeView1.GetNodeAt(e.X, e.Y);
          if(tn != null)
          {
               int currentNodeIndex = tn.Index;
               if(currentNodeIndex != oldNodeIndex)
               {
                    oldNodeIndex = currentNodeIndex;
                    if(this.toolTip1 != null && this.toolTip1.Active)
                         this.toolTip1.Active = false; //turn it off

                    this.toolTip1.SetToolTip(this.treeView1, string.Format("tooltip: node {0}", oldNodeIndex));
                    this.toolTip1.Active = true; //make it active so it can show
               }
          }
     }

[VB.NET]
     Private oldNodeIndex As Integer = - 1
     Private toolTip1 As ToolTip


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


     Private Sub treeView1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs)
          Dim tn As TreeNode = Me.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 (Me.toolTip1 Is Nothing) And Me.toolTip1.Active Then
                    Me.toolTip1.Active = False 'turn it off
               End If
               Me.toolTip1.SetToolTip(Me.treeView1, String.Format("tooltip: node {0}", oldNodeIndex))
               Me.toolTip1.Active = True 'make it active so it can show
               End If
          End If
     End Sub 'treeView1_MouseMove