I have constructed a TreeView with varying levels of parent and child nodes.
My challenge now is that I do not know how to write the contents of my TreeView to a text file. I would also like to be able to read the text file to again re-populate my TreeView.
How can I go about this?
Any ideas and/or code snippets would be greatly appreciated. I am using Visual Basic 2010 Express. Thank you in advance.
There's a number of ways that you could accomplish this but I believe the best way would be to save the treeview nodes as binary data to a flat file and read it back when you need it.
Here is a simple example. You can create a new vb.net winforms project and just copy/paste this code over the code for Form1
and click 'Run':
Public Class Form1
Dim tv As New TreeView
Dim cmdSave As New Button
Dim cmdClear As New Button
Dim cmdLoad As New Button
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Setup control positions and values
tv.Location = New Point(0, 0)
tv.Size = New Size(Me.Width, Me.ClientSize.Height - cmdSave.Height)
cmdSave.Location = New Point(0, Me.ClientSize.Height - cmdSave.Height)
cmdSave.Text = "Save"
AddHandler cmdSave.Click, AddressOf cmdSave_Click
cmdClear.Location = New Point(cmdSave.Left + cmdSave.Width, Me.ClientSize.Height - cmdClear.Height)
cmdClear.Text = "Clear"
AddHandler cmdClear.Click, AddressOf cmdClear_Click
cmdLoad.Location = New Point(cmdClear.Left + cmdClear.Width, Me.ClientSize.Height - cmdLoad.Height)
cmdLoad.Text = "Load"
AddHandler cmdLoad.Click, AddressOf cmdLoad_Click
' Build the treeview
tv.Nodes.Add("Node 1")
tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 1 - Child 1")
tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 1 - Child 2")
tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 1 - Child 3")
tv.Nodes.Add("Node 2")
tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 2 - Child 1")
tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 2 - Child 2")
tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 2 - Child 3")
tv.ExpandAll()
' Add controls to the form
Me.Controls.Add(tv)
Me.Controls.Add(cmdSave)
Me.Controls.Add(cmdClear)
Me.Controls.Add(cmdLoad)
End Sub
Private Sub cmdSave_Click(sender As Object, e As EventArgs)
Try
Dim dlg As New SaveFileDialog
dlg.DefaultExt = "sav"
dlg.Filter = "sav files|*.sav"
dlg.OverwritePrompt = True
If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
' Save the treeview nodes to file
Dim oTreeList As New List(Of clsTreeFile)
For Each oNode As TreeNode In tv.Nodes
Dim oTree As New clsTreeFile
oTree.oTreeNode = oNode
oTreeList.Add(oTree)
Next
Using oFileStream As IO.FileStream = IO.File.Open(dlg.FileName, IO.FileMode.Create)
Dim oBinaryFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
oBinaryFormatter.Serialize(oFileStream, oTreeList)
End Using
MessageBox.Show("Treeview saved successfully.", "File saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MessageBox.Show("An error occurred while saving:" & vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub cmdClear_Click(sender As Object, e As EventArgs)
tv.Nodes.Clear()
End Sub
Private Sub cmdLoad_Click(sender As Object, e As EventArgs)
Dim dlg As New OpenFileDialog
Try
dlg.DefaultExt = "sav"
dlg.Filter = "SAV files|*.sav"
If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
' Open saved file and read the binary data back to the treeview
tv.Nodes.Clear()
Dim oTreeList As New List(Of clsTreeFile)
Using oFileStream As IO.FileStream = IO.File.Open(dlg.FileName, IO.FileMode.Open)
Dim oBinaryFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
oTreeList = CType(oBinaryFormatter.Deserialize(oFileStream), List(Of clsTreeFile))
End Using
For Each oNode As clsTreeFile In oTreeList
tv.Nodes.Add(oNode.oTreeNode)
Next
tv.ExpandAll()
End If
Catch ex As Exception
MessageBox.Show("The " & System.IO.Path.GetFileName(dlg.FileName) & " file cannot be opened." & vbCrLf & vbCrLf & ex.Message, "Error opening file", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
<Serializable()> _
Public Class clsTreeFile
Public oTreeNode As TreeNode
End Class