如何将树视图的内容写入文本文件
How to write the contents of a treeview to a text file
我构建了一个具有不同级别的父节点和子节点的 TreeView。
我现在的挑战是我不知道如何将 TreeView 的内容写入文本文件。我还希望能够读取文本文件以再次重新填充我的 TreeView。
我该怎么做?
任何想法 and/or 代码片段将不胜感激。我正在使用 Visual Basic 2010 Express。提前谢谢你。
有多种方法可以实现此目的,但我认为最好的方法是将树视图节点作为二进制数据保存到平面文件中,并在需要时读回。
这是一个简单的例子。您可以创建一个新的 vb.net winforms 项目,只需 copy/paste 此代码覆盖 Form1
的代码,然后单击 '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
我构建了一个具有不同级别的父节点和子节点的 TreeView。
我现在的挑战是我不知道如何将 TreeView 的内容写入文本文件。我还希望能够读取文本文件以再次重新填充我的 TreeView。
我该怎么做?
任何想法 and/or 代码片段将不胜感激。我正在使用 Visual Basic 2010 Express。提前谢谢你。
有多种方法可以实现此目的,但我认为最好的方法是将树视图节点作为二进制数据保存到平面文件中,并在需要时读回。
这是一个简单的例子。您可以创建一个新的 vb.net winforms 项目,只需 copy/paste 此代码覆盖 Form1
的代码,然后单击 '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