Importing tooltip text from excel sheet in Treeview

haplc

Board Regular
Joined
May 27, 2004
Messages
71
Dear All,

I would like to populate a treeview using Columns from excel sheet. Col A is having parent name, Col B is having child name and Col C is having tool tip for each Child and parent. I am able to import parent and child in treeview but could not manage to import tool tip. I thanks in advance for support.

Code used is:

Private Sub UserForm_Initialize()

'for excel to treeview

TreeView2.LineStyle = tvwRootLines ' showing + sign in treeview
Dim c As Range
Dim nParent As node
Dim nChild As node

'ON error next
On Error Resume Next

'Parents are stored in Col A and child are stored in col B . Tool tip in Col C
'getting parent name from Col A
For Each c In Sheet3.Range("a1:a" & Range("a" & Rows.Count).End(xlUp).Row)

Set nParent = TreeView2.Nodes.Add(, , c.Value, c.Value)

'Ensureing no error is there
If Err.Number <> 0 Then
'Error reset and setting parent
Err.Clear
Set nParent = TreeView2.Nodes(c.Value)
End If

'getting Child name from Col B

Set nChild = TreeView2.Nodes.Add(nParent, tvwChild, , c.Offset(0, 1).Value)



Err.Clear
Next
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Here is an example; note that for the screen tip to update, the mouse pointer has to leave the tree view area. Because of that, I added a label that updates as soon as the mouse hovers a node.

uform2021.PNG


VBA Code:
' standard module
Private Declare Function GetDC Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" _
(ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare Function ReleaseDC Lib "user32" _
(ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Const LOGPIXELSX = 88 'Pixels/inch in X
'A point is defined as 1/72 inches
Private Const POINTS_PER_INCH As Long = 72

'The size of a pixel, in points
Public Function PointsPerPixel#()
Dim hDC&, dpi&
hDC = GetDC(0)
dpi = GetDeviceCaps(hDC, LOGPIXELSX)
PointsPerPixel = POINTS_PER_INCH / dpi
ReleaseDC 0, hDC
End Function

VBA Code:
Private Sub UserForm_Initialize()
Dim c As Range, Par As Node, nChild As Node, gc As MSComctlLib.Node
Me.Label1.Font.Size = 14
TreeView1.LineStyle = tvwRootLines
Me.TreeView1.LabelEdit = tvwManual
For Each c In Sheet1.Range("a1:a" & Range("a" & Rows.Count).End(xlUp).Row)
    Set Par = TreeView1.Nodes.Add(, , CStr(c.Offset(, 2)), c.Value)
    If Err.Number <> 0 Then
        Err.Clear
        Set Par = TreeView1.Nodes(c.Value)
    End If
    Set nChild = TreeView1.Nodes.Add(Par, 4, CStr(c.Offset(, 2)) & " - child1", c.Offset(0, 1))
    Set gc = TreeView1.Nodes.Add(nChild, tvwChild, c.Offset(, 1) & " - child2", "grandchild")
    Err.Clear
Next
End Sub

Private Sub treeview1_MouseMove(ByVal Button As Integer, ByVal Shift%, _
ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
Dim nde As Node, f#
f = PointsPerPixel * 20
If Not (TreeView1.HitTest(x * f, y * f) Is Nothing) Then
    Set nde = TreeView1.HitTest(x * f, y * f)
    nde.Bold = True
    TreeView1.ControlTipText = nde.Key
    Me.Label1 = CStr(nde.Key)
    Me.Repaint
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top