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
Printable View
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
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:
This'll serialize the nodes and the tag, if possible directly to a stream...and not the TreeView itself.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
Thanks I am looking code in c#
Oh..sorry about that, sometimes I confuse myself as I jump between the forums :blush:
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);
}
Thanks I will update you soon
Hey thanks but here is some modification - A treeview is N-deep; this is 1-deep.
How do you mean? My code should serialize all "levels" of nodes, no matter how deeply nested they are.
ok thanks