Save/Load Treeview

  • Thread starter Thread starter Mr.D
  • Start date Start date
M

Mr.D

How do I save/load the contents of a Treeview to a file?
I have found several good examples written i VB6, but not a single one for
VB.NET.

Please help.
 
Mr.D,

There are so much samples for loading a Treeview, I think you need a very
special when no one did fit you.

What is so special that no one was good?

Cor
 
Cor Ligthert said:
There are so much samples for loading a Treeview, I think you need a very
special when no one did fit you.

I diddn't find a single Load+Save example for VB.NET
What is so special that no one was good?
Nothing special. I dont really care how the contenst are stored.

I've searched on
http://www.planet-source-code.com/
http://www.google.com/
and the Community search in MSDN 2005 Express and I came up whith nothing.

As said, I found many good VB6 examples, but I dont have the skilles of
rewriting them in .NET.
That's why I cried out for help in here.
 
How do I save/load the contents of a Treeview to a file?
I have found several good examples written i VB6, but not a single one for
VB.NET.

Can't you download the vb6 version and use the upgrade wizard to
convert it for you??
 
Mr. D.

The threeview is not really a file related control, when you want to do it
you have to take the nodes from the tree and write them, therefore you will
probably not get easily a sample.

However what you ask looks directly to using a treeview with xmldoc

Beneath the links where I have searched this newsgroup for "treeview and
xml", there should be an example between it. It do not have it myself at the
moment.

http://tinyurl.com/4kpmb

I hope this helps?

Cor
 
Mr.D said:
I diddn't find a single Load+Save example for VB.NET

Nothing special. I dont really care how the contenst are stored.

I've searched on
http://www.planet-source-code.com/
http://www.google.com/
and the Community search in MSDN 2005 Express and I came up whith nothing.

As said, I found many good VB6 examples, but I dont have the skilles of
rewriting them in .NET.
That's why I cried out for help in here.

Hi Tim,

here is an example inC#
http://www.codeproject.com/cs/miscctrl/loadandsave.asp
with http://www.kamalpatel.net/ConvertCSharp2VB.aspx convert it to VB.net

Greeting

Thomas
 
How do I save/load the contents of a Treeview to a file?
I have found several good examples written i VB6, but not a single one for
VB.NET.

Please help.

The following uses a couple of strucures to represent the Treeview in
it's simplest form and then serialization to persist the data to file:

<Code>

<Serializable()> Public Structure TreeViewData
Public Nodes() As TreeNodeData

Public Sub New(ByVal treeview As TreeView)
If treeview.Nodes.Count = 0 Then Exit Sub

ReDim Nodes(treeview.Nodes.Count - 1)
For i As Integer = 0 To treeview.Nodes.Count - 1
Nodes(i) = New TreeNodeData(treeview.Nodes(i))
Next

End Sub

Public Sub PopulateTree(ByVal treeview As TreeView)

If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit
Sub
For i As Integer = 0 To Me.Nodes.Length - 1
treeview.Nodes.Add(Me.Nodes(i).ToTreeNode)
Next

End Sub

End Structure

<Serializable()> Public Structure TreeNodeData

Public Text As String
Public ImageIndex As Integer
Public SelectedImageIndex As Integer
Public Nodes() As TreeNodeData

Public Sub New(ByVal node As TreeNode)

Me.Text = node.Text
Me.ImageIndex = node.ImageIndex
Me.SelectedImageIndex = node.SelectedImageIndex

If node.Nodes.Count = 0 Then Exit Sub

ReDim Nodes(node.Nodes.Count - 1)
For i As Integer = 0 To node.Nodes.Count - 1
Nodes(i) = New TreeNodeData(node.Nodes(i))
Next

End Sub

Public Function ToTreeNode() As TreeNode
ToTreeNode = New TreeNode(Me.Text, Me.ImageIndex,
Me.SelectedImageIndex)

If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit
Function
For i As Integer = 0 To Me.Nodes.Length - 1
ToTreeNode.Nodes.Add(Me.Nodes(i).ToTreeNode)
Next
End Function

End Structure

</Code>

Then to use it:

<Code>

Private SubLoadButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button3.Click

Dim ser As New
System.Xml.Serialization.XmlSerializer(GetType(TreeViewData))
Dim file As New System.IO.FileStream("C:\Temp\TreeView.xml",
IO.FileMode.OpenOrCreate)
Dim writer As New System.Xml.XmlTextWriter(file, Nothing)

ser.Serialize(writer, New TreeViewData(TreeView1))

writer.Close()
file.Close()
file = Nothing

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button4.Click

Dim ser As New
System.Xml.Serialization.XmlSerializer(GetType(TreeViewData))
Dim file As New System.IO.FileStream("C:\Temp\TreeView.xml",
IO.FileMode.Open)
Dim reader As New System.Xml.XmlTextReader(file)

Dim treeData As TreeViewData = CType(ser.Deserialize(reader),
TreeViewData)
treeData.PopulateTree(TreeView1)

reader.Close()
file.Close()
file = Nothing

End Sub

</Code>

Hope this helps

Blu.
 
The following uses a couple of strucures to represent the Treeview in
it's simplest form and then serialization to persist the data to file:

Sorry... couple of errors there on testing... try this.

<Serializable()> Public Structure TreeViewData
Public Nodes() As TreeNodeData

Public Sub New(ByVal treeview As TreeView)
If treeview.Nodes.Count = 0 Then Exit Sub

ReDim Nodes(treeview.Nodes.Count - 1)
For i As Integer = 0 To treeview.Nodes.Count - 1
Nodes(i) = New TreeNodeData(treeview.Nodes(i))
Next

End Sub

Public Sub PopulateTree(ByVal treeview As TreeView)

If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit
Sub
For i As Integer = 0 To Me.Nodes.Length - 1
treeview.Nodes.Add(Me.Nodes(i).ToTreeNode)
Next

End Sub

End Structure

<Serializable()> Public Structure TreeNodeData

Public Text As String
Public ImageIndex As Integer
Public SelectedImageIndex As Integer
Public Nodes() As TreeNodeData

Public Sub New(ByVal node As TreeNode)

Me.Text = node.Text
Me.ImageIndex = node.ImageIndex
Me.SelectedImageIndex = node.SelectedImageIndex

If node.Nodes.Count = 0 Then Exit Sub

ReDim Nodes(node.Nodes.Count - 1)
For i As Integer = 0 To node.Nodes.Count - 1
Nodes(i) = New TreeNodeData(node.Nodes(i))
Next

End Sub

Public Function ToTreeNode() As TreeNode
ToTreeNode = New TreeNode(Me.Text, Me.ImageIndex,
Me.SelectedImageIndex)

If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit
Function
For i As Integer = 0 To Me.Nodes.Length - 1
ToTreeNode.Nodes.Add(Me.Nodes(i).ToTreeNode)
Next
End Function

End Structure>
</Code>

Then to use it:

<Code>
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles SaveButton.Click

Dim ser As New
System.Xml.Serialization.XmlSerializer(GetType(TreeViewData))
Dim file As New System.IO.FileStream("C:\Temp\TreeView.xml",
IO.FileMode.Create)
Dim writer As New System.Xml.XmlTextWriter(file, Nothing)

ser.Serialize(writer, New TreeViewData(TreeView1))

writer.Close()
file.Close()
file = Nothing

End Sub

Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles LoadButton.Click

Dim ser As New
System.Xml.Serialization.XmlSerializer(GetType(TreeViewData))
Dim file As New System.IO.FileStream("C:\Temp\TreeView.xml",
IO.FileMode.Open)
Dim reader As New System.Xml.XmlTextReader(file)

Dim treeData As TreeViewData = CType(ser.Deserialize(reader),
TreeViewData)
treeData.PopulateTree(TreeView1)

reader.Close()
file.Close()
file = Nothing

End Sub
 
BluDog said:
The following uses a couple of strucures to represent the Treeview in
it's simplest form and then serialization to persist the data to file:

<Code></Code>

Outstanding code!
It ran directly out of the box :o)

Thanks for the help!
Hope this helps
Blu.

You are a lifesaver.
 
Mr.D said:
"thomas wenning" (e-mail address removed) wrote

Thanks Thomas.
I don't know C# but it looks very promissing indeed.
VB.net

The converter do convert the code, but unfortunatly I get errors when
pasting the code into VB.

Hi Tim,

you can bind this c#-control to your project.

Regards

Thomas Wenning
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top