What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleChange a TreeView control's tooltip when the mouse moves over different nodes
DescriptionThis example shows how to change a TreeView control's tooltip when the mouse moves over different nodes in Visual Basic 6. When the user moves the mouse over a new node, the program uses the control's HitText method to see what node is under the mouse and then sets the control's ToolTipText property accordingly.
KeywordsTreeView, tooltip, TreeView tooltip, ToolTipText
The TreeView control's MouseMove event handler determines whether the left button is down. If it is, then the program selects the node under the mouse and begins a drag. If the left mouse button is not pressed, then the event handler calls subroutine SetToolTip.

SetToolTip sees if the mouse has moved since it was last called. If it has moved, then the code uses the HitTest method to see what node is under the mouse. If this is not the same node that was under the mouse last time, then the program sets the TreeView control's ToolTipText property to the node's name.

Private Sub OrgTree_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
        ' Start a new drag. Note that we do not get
        ' other MouseMove events while the drag is
        ' in progress.
        ' See what node we are dragging.
        SourceType = NodeType(SourceNode)

        ' Select this node. When no node is highlighted,
        ' this node will be displayed as selected. That
        ' shows where it will land if dropped.
        Set OrgTree.SelectedItem = SourceNode

        ' Set the drag icon for this source.
        OrgTree.DragIcon = IconImage(SourceType)
        OrgTree.Drag vbBeginDrag
    ElseIf Button = 0 Then
        SetToolTip X, Y
    End If
End Sub

' If the mouse has moved, change the tool tip.
Private Sub SetToolTip(ByVal X As Single, ByVal Y As Single)
Static last_x As Single
Static last_y As Single
Static node_over As Node
Dim new_node_over As Node

    ' See if the mouse has moved.
    If (last_x = X) And (last_y = Y) Then Exit Sub
    last_x = X
    last_y = Y

    ' See what node we're over.
    Set new_node_over = OrgTree.HitTest(X, Y)
    If node_over Is new_node_over Then Exit Sub
    Set node_over = new_node_over

    ' Set the tool tip text to be the node's key minus
    ' the first 2 characters (which give its type).
    If new_node_over Is Nothing Then
        OrgTree.ToolTipText = ""
        OrgTree.ToolTipText = Mid$(node_over.Key, 3)
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.