View Full Version : [SOLVED:] Tree View Control in a UserForm
gsouza
12-22-2004, 07:18 AM
Just trying to figure out how this control works. Would anybody have a sample file with a Tree View Control in a UserForm that they would like to share. If so i would greatly appreciate it.
Jacob Hilderbrand
12-23-2004, 02:17 AM
Here is an example. Create a User Form with a TreeView Control and paste this code. Or check out the attached example.
In the example I am just adding the text like ("Node - " & i) but you could also get the text from cells in a worksheet.
I am also changing the User Form caption based on the node that is clicked. This is just to demonstrate how to determine what node was clicked. You could then have the different nodes do various things when they are clicked.
Option Explicit
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Me.Caption = Node.Text
End Sub
Private Sub UserForm_Initialize()
Dim i As Long
Dim j As Long
Dim k As Long
Dim Title As String
Dim xNode As Node
Dim NodeKey As String
Dim NodeKey2 As String
With Me.TreeView1
For i = 1 To 5
Set xNode = .Nodes.Add
NodeKey = "Node - " & i
With xNode
.Key = NodeKey
.Text = "Node - " & i
.Expanded = False
End With
For j = 1 To 7
Set xNode = .Nodes.Add(NodeKey, tvwChild)
NodeKey2 = "Node - Child - " & i & j
With xNode
.Key = NodeKey2
.Text = "Child - " & j
End With
For k = 1 To 10
Set xNode = .Nodes.Add(NodeKey2, tvwChild)
With xNode
.Text = "Child2 - " & k
End With
Next k
Next j
Next i
End With
Set xNode = Nothing
End Sub
gsouza
12-23-2004, 06:43 AM
Awesome thanks DRJ. I am going to play around with it.
Jacob Hilderbrand
12-23-2004, 11:09 AM
You're Welcome
Take Care
gsouza
12-23-2004, 11:16 AM
Happy Holidays!!!
benabd
09-29-2006, 03:41 AM
Hi All,
Is it possible to have the same code but instead of clicking on the node, the user just needs to hover the mouse over the TreeView to have the current node text shown in the userform caption.
Thanks for your help.:help
Regards,
Karim
Andy Pope
09-30-2006, 04:19 AM
See attached example which uses DRJ's example and the HitTest function.
Module Code
Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, _
ByVal hdc As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, _
ByVal nIndex As Long) As Long
Const HWND_DESKTOP As Long = 0
Const LOGPIXELSX As Long = 88
Const LOGPIXELSY As Long = 90
'--------------------------------------------------
Function TwipsPerPixelX() As Single
'--------------------------------------------------
'Returns the width of a pixel, in twips.
'--------------------------------------------------
Dim lngDC As Long
lngDC = GetDC(HWND_DESKTOP)
TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
ReleaseDC HWND_DESKTOP, lngDC
End Function
'--------------------------------------------------
Function TwipsPerPixelY() As Single
'--------------------------------------------------
'Returns the height of a pixel, in twips.
'--------------------------------------------------
Dim lngDC As Long
lngDC = GetDC(HWND_DESKTOP)
TwipsPerPixelY = 1440& / GetDeviceCaps(lngDC, LOGPIXELSY)
ReleaseDC HWND_DESKTOP, lngDC
End Function
Userform code
Private Sub TreeView1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, _
ByVal y As stdole.OLE_YPOS_PIXELS)
Dim nodTemp As Node
Dim strText As String
If Button = 0 Then
Set nodTemp = TreeView1.HitTest( _
TwipsPerPixelX * x, TwipsPerPixelY * y)
If Not nodTemp Is Nothing Then
strText = nodTemp.Text
Else
strText = "Nothing"
End If
Me.Caption = "X=" & x & " Y=" & y & " " & strText
End If
End Sub
benabd
10-02-2006, 03:13 AM
Thanks Andy for your help. :friends:
It works just fine.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.