VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



TreeView Export Routine

by Levi (2 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

Exports a TreeView control (including ALL it's children) into a graphical representation with plain text.

Inputs
A tree control!
Assumes
You'll never find anything 'easier. just paste the code into your 'program and call the first function 'and it will return the output of 'everything in your TreeView control 'Ex:Text1.Text = exportTree(MyTreeView)
Code Returns
A string with the formatted 'text to look like the tree control

Rate TreeView Export Routine

'sample output:
'
'+- 07/21/01 - 11:57:09 PM
'| \- Shell_TrayWnd - ""
'| |- Button - ""
'| |- TrayNotifyWnd - ""
'| | \- TrayClockWClass - ""
'| |- MSTaskSwWClass - ""
'| \- SysTabControl32 - ""
'**************************************
Function exportTree(tree As TreeView) As String
 Dim txtlen As Long, txtbuffer As String, index As Long
 Dim vlines() As Boolean, ret as String
 ret = chr(13) & chr(10)
 ReDim Preserve vlines(0)
 vlines(0) = True
 numchildren = 0
 exportTree = "+- " & Format(Date, "mm/dd/yy") & " - " & Format(Time, "hh:mm:ss AM/PM") & " - "
 Dim depth As Integer, start As Long
 index = Tree.Nodes.Item(1).FirstSibling.index
 Do
 On Error Resume Next
 Err.Clear
 index2 = Tree.Nodes.Item(index).Next.index
 If Err.Number = 91 Then
 On Error GoTo 0
 vlines(0) = False
 exportTree = exportTree & ret & "| \- " & Tree.Nodes.Item(index).Text _
 & getchildren(index, 0, vlines)
 Exit Do
 Else
 On Error GoTo 0
 exportTree = exportTree & ret & "| |- " & Tree.Nodes.Item(index).Text _
 & getchildren(index, 0, vlines)
 index = Tree.Nodes.Item(index).Next.index
 End If
 Loop
 exportTree = exportTree & ret & "|" & ret & "|" & ret & "|" & ret & "|"
End Function
'the following function calls itself over and over for each child and returns with
'ALL of the children and their children, etc. of the current item in the tree
Function getchildren(ByVal index As Long, ByVal childcnt As Integer, ByRef vlines() As Boolean, Optional data As String = "") As String
 Dim children As Integer
 childcnt = childcnt + 1
 ReDim Preserve vlines(childcnt)
 children = Tree.Nodes.Item(index).children
 If children > 1 Then
 vlines(childcnt) = True
 data = data & ret & childspaces(childcnt, vlines) & "|- " & Tree.Nodes.Item(index).Child
 Call getchildren(Tree.Nodes.Item(index).Child.index, childcnt, vlines, data)
 index = Tree.Nodes.Item(index).Child.index
 
 For i% = 3 To children
 data = data & ret & childspaces(childcnt, vlines) & "|- " & Tree.Nodes.Item(index).Next
 Call getchildren(Tree.Nodes.Item(index).Next.index, childcnt, vlines, data)
 index = Tree.Nodes.Item(index).Next.index
 Next i%
 
 vlines(childcnt) = False
 data = data & ret & childspaces(childcnt, vlines) & "\- " & Tree.Nodes.Item(index).Next
 Call getchildren(Tree.Nodes.Item(index).Next.index, childcnt, vlines, data)
 
 ElseIf children = 1 Then
 vlines(childcnt) = False
 data = data & ret & childspaces(childcnt, vlines) & "\- " & Tree.Nodes.Item(index).Child
 Call getchildren(Tree.Nodes.Item(index).Child.index, childcnt, vlines, data)
 End If
 getchildren = data
End Function
'This function is used to insert the correct amount of space from the edge
'to make all the children line up properly
Function childspaces(childcnt As Integer, vlines() As Boolean) As String
 childspaces$ = "| "
 For i% = 1 To childcnt
 childspaces$ = childspaces$ & IIf(vlines(i% - 1), "| ", " ")
 Next i%
End Function

Download this snippet    Add to My Saved Code

TreeView Export Routine Comments

No comments have been posted about TreeView Export Routine. Why not be the first to post a comment about TreeView Export Routine.

Post your comment

Subject:
Message:
0/1000 characters