2015-04-16 71 views
0

我已经构建了具有不同级别的父节点和子节点的TreeView。如何将树视图的内容写入文本文件

我现在面临的挑战是我不知道如何将我的TreeView的内容写入文本文件。我也希望能够读取文本文件以再次重新填充我的TreeView。

我该怎么办?

任何想法和/或代码片段将不胜感激。我正在使用Visual Basic 2010 Express。先谢谢你。

回答

1

有很多方法可以实现这一点,但我相信最好的方法是将treeview节点作为二进制数据保存到平面文件并在需要时将其读回。

这是一个简单的例子。您可以创建一个新的vb.net WinForms项目,只是复制/粘贴此代码在代码Form1,然后单击“运行”:

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 
+0

这对我非常有用,我会尝试一下,让你知道它是如何做的。 – Iki

+0

非常感谢您的协助。这完美运行。在这里和那里进行了一些调整,我可以对其进行修改以适合我的应用程序。再一次感谢你。你帮了我很多。 – Iki

相关问题