|
-
Mar 24th, 2009, 01:56 PM
#1
Thread Starter
Just Married
[RESOLVED] Treeview 3.5
Hello friends how to serializes a System.Windows.Forms.TreeView, both node and .Tag information, and returns a byte array. same for it's opposite how to deserializes a byte array to a System.Windows.Form.TreeView.
Thanks
Shakti
-
Mar 24th, 2009, 02:37 PM
#2
Re: Treeview 3.5
The TreeView is not serializable, so it can not be seiralized. The TreeNode class can be serialized however, but the TreeNodeCollection can not.
Heres what I believe you'd need to do:
Code:
Private Sub SerializeTreeViewInfo(ByVal instance As TreeView, ByVal output As Stream)
Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim nodes As New List(Of TreeNode)
For i As Integer = 0 To instance.Nodes.Count - 1
nodes.Add(instance.Nodes(i))
Next
formatter.Serialize(output, nodes)
If instance.Tag.GetType().IsSerializable Then
formatter.Serialize(output, instance.Tag)
End If
End Sub
Private Sub DeserializeTreeViewInfo(ByVal instance As TreeView, ByVal input As Stream)
Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim nodes As List(Of TreeNode)
nodes = DirectCast(formatter.Deserialize(input), List(Of TreeNode))
For i As Integer = 0 To nodes.Count - 1
instance.Nodes.Add(nodes(i))
Next
If input.Position <> input.Length Then
instance.Tag = formatter.Deserialize(input)
End If
End Sub
This'll serialize the nodes and the tag, if possible directly to a stream...and not the TreeView itself.
-
Mar 24th, 2009, 03:03 PM
#3
Thread Starter
Just Married
Re: Treeview 3.5
Thanks I am looking code in c#
-
Mar 24th, 2009, 03:18 PM
#4
Re: Treeview 3.5
 Originally Posted by shakti5385
Thanks I am looking code in c#
Oh..sorry about that, sometimes I confuse myself as I jump between the forums 
Code:
private void SerializeTreeViewInfo(TreeView instance, System.IO.Stream output){
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
List<TreeNode> nodes = new List<TreeNode>();
for(int i = 0; i < instance.Nodes.Count; i++)
nodes.Add(instance.Nodes[i]);
formatter.Serialize(output, nodes);
if(instance.Tag.GetType().IsSerializable)
formatter.Serialize(output, instance.Tag);
}
private void DeserializeTreeViewInfo(TreeView instance, System.IO.Stream input){
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
List<TreeNode> nodes;
nodes = (List<TreeNode>)formatter.Deserialize(input);
for(int i = 0; i < nodes.Count; i++)
instance.Nodes.Add(nodes[i]);
if(input.Position != input.Length)
instance.Tag = formatter.Deserialize(input);
}
-
Mar 24th, 2009, 03:45 PM
#5
Thread Starter
Just Married
Re: Treeview 3.5
Thanks I will update you soon
-
Mar 25th, 2009, 09:33 AM
#6
Thread Starter
Just Married
Re: Treeview 3.5
Hey thanks but here is some modification - A treeview is N-deep; this is 1-deep.
-
Mar 25th, 2009, 09:39 AM
#7
Re: Treeview 3.5
How do you mean? My code should serialize all "levels" of nodes, no matter how deeply nested they are.
-
Mar 29th, 2009, 02:25 AM
#8
Thread Starter
Just Married
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|