Microsoft KB Archive/267909

= How To Save TreeView Data to a Formatted File =

Article ID: 267909

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q267909



SUMMARY
The TreeView control does not offer built-in functionality to save its contents to a file. To generate this behavior, a routine can be used recursively to write the data from a TreeView control to a file and maintain the visual layout.



MORE INFORMATION
The steps included here create a project that saves the text property of each node in a TreeView control to a file. The saved nodes are indented in the file to mimic the layout of the TreeView as displayed on the Form.

Step-by-Step Example
 Start a new Standard EXE project in Visual Basic. Form1 is created by default. Set the BorderStyle property of Form1 to 4 - FixedToolWindow. On the Project menu, click to select Components. In the Components dialog box, select the Microsoft Windows Common Controls 6.0, and then click OK. Add one TreeView and one CommandButton control to Form1.  Add the following code to the General Declarations section of Form1: Option Explicit

Const strFile = &quot;c:\tvn_tree.txt&quot; Const intIndentSpaces = 4

Dim ff As Integer Dim intIndentLevel As Integer

Private Sub Command1_Click Dim objNode As Node ' Open up a text file to export the ' indented TreeView nodes into. ff = FreeFile Open strFile For Output As #ff

' Set the indent for the root node intIndentLevel = 0 ' Set the root node Set objNode = TreeView1.Nodes(1) ' Call the print routine with the root node ParseTree objNode

' Close the text file Close #ff End Sub

Private Sub Form_Load ' Layout Form User Interface Form1.Caption = &quot;Export TreeView&quot; Form1.Move 0, 0, 4140, 4425 TreeView1.Move 240, 240, 3500, 2895 Command1.Move 240, 3350, 3500, 495 Command1.Caption = &quot;Save Treeview nodes to file: &quot; & strFile

' Add data to the TreeView control TreeView1.LineStyle = tvwRootLines TreeView1.Nodes.Add, , &quot;TopLevelBranch1&quot;, &quot;TopLevelBranch1&quot; TreeView1.Nodes.Add &quot;TopLevelBranch1&quot;, tvwChild, &quot;SubBranch1_1&quot;, &quot;SubBranch1_1&quot; TreeView1.Nodes.Add &quot;SubBranch1_1&quot;, tvwChild, &quot;Node1_1_1&quot;, &quot;Node1_1_1&quot; TreeView1.Nodes.Add &quot;SubBranch1_1&quot;, tvwChild, &quot;Node1_1_2&quot;, &quot;Node1_1_2&quot; TreeView1.Nodes.Add &quot;SubBranch1_1&quot;, tvwChild, &quot;Node1_1_3&quot;, &quot;Node1_1_3&quot; TreeView1.Nodes.Add &quot;TopLevelBranch1&quot;, tvwChild, &quot;SubBranch1_2&quot;, &quot;SubBranch1_2&quot; TreeView1.Nodes.Add &quot;SubBranch1_2&quot;, tvwChild, &quot;Node1_2_1&quot;, &quot;Node1_2_1&quot; TreeView1.Nodes.Add &quot;SubBranch1_2&quot;, tvwChild, &quot;Node1_2_2&quot;, &quot;Node1_2_2&quot; TreeView1.Nodes.Add, , &quot;TopLevelBranch2&quot;, &quot;TopLevelBranch2&quot; TreeView1.Nodes.Add &quot;TopLevelBranch2&quot;, tvwChild, &quot;SubBranch2_1&quot;, &quot;SubBranch2_1&quot; TreeView1.Nodes.Add &quot;TopLevelBranch2&quot;, tvwChild, &quot;SubBranch2_2&quot;, &quot;SubBranch2_2&quot; TreeView1.Nodes.Add &quot;SubBranch2_2&quot;, tvwChild, &quot;Node2_2_1&quot;, &quot;Node2_2_1&quot; End Sub

Sub ParseTree(objNode As Node) ' Print the node that was passed in and ' account for the node's level Print #ff, Space(intIndentLevel * intIndentSpaces) & objNode.Text ' Check to see if the current node has children If objNode.Children > 0 Then ' Increase the indent if children exist intIndentLevel = intIndentLevel + 1 ' Pass the first child node to the print routine ParseTree objNode.Child End If   ' Set the next node to print Set objNode = objNode.Next ' As long as we have not reached the last node in   ' a branch, continue to call the print routine If TypeName(objNode) <> &quot;Nothing&quot; Then ParseTree objNode Else ' If the last node of a branch was reached, ' decrease the indentation counter intIndentLevel = intIndentLevel - 1 End If End Sub  Run the project and click the button to save the TreeView data to a file named tvn_tree.txt that resides in the root directory of the C: drive. If the newly created file is opened in a text editor such as Notepad, note that the formatting mimics the TreeView layout.</ol>

<div class="references_section">