mntlnstituteflr Posted February 6, 2006 Posted February 6, 2006 Hello, I am making an application again and I have come to a problem. I can not figure out how to make a Tree View open and save their data as a custom file format. :confused: What I have tried is... Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then TreeView1.SaveFile(SaveFileDialog1.FileName) End If End Sub That doesn't work and I have tried other codings such as converting it to plain text, which obviously didn't work either... Can you help me? Patrick Quote
Leaders snarfblam Posted February 7, 2006 Leaders Posted February 7, 2006 You will have to iterate through the tree nodes and save them yourself. Intellisense or MSDN will make it adequately clear that there is no built-in method to save the contents of a treeview, as you will find with the majority of controls. Sub SaveTree 'You may need to add/change parameters Dim MyFileStream As New FileStream("C:\MyFile.extension") Dim MyWriter As New BinaryWriter(MyFileStream) MyWriter.Write(MyTree.Nodes.Count) For I As Integer = 0 To MyTree.Nodes.Count - 1 SaveNode(MyWriter, MyTree.Nodes(i)) Next I End Sub Sub SaveNode(BinaryWriter b, TreeNode t) b.Write(t.Text) b.Write(t.Nodes.Count) For I As Integer = 0 To t.Nodes.Count - 1 SaveNode(b, t.Nodes(i)) Next End Sub Sub LoadTree 'You may need to add/change parameters Dim MyFileStream As New FileStream("C:\MyFile.extension") Dim MyReader As New BinaryReader(MyFileStream) Dim NodeCount As Integer = MyReader.ReadInt32() For I As Integer = 0 To NodeCount - 1 MyTree.Nodes.Add(LoadNode(MyReader)) Next I End Sub Function LoadNode(BinaryReader b, TreeNode t) As TreeNode Dim Text As String = b.ReadString() Dim NodeCount As Intger = b.ReadInt32() Dim ResultNode As New TreeNode(Text) For I As Integer = 0 To NodeCount ResultNode.Nodes.Add(LoadNode(b)) Next Return ResultNode End Sub That code most likely won't compile or run, but it demonstrates the basic process of saving the contents of a hierarchal structure using recursion. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.