2015-04-16 72 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

相關問題