Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. serialize an object for use by drag & drop

serialize an object for use by drag & drop

Scheduled Pinned Locked Moved C#
data-structuresjsonhelpquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    misterbear
    wrote on last edited by
    #1

    Hello, I posted a similar question a couple of days ago, got some advice but don't seem to be able to make this one work anyway. Here is the problem: I have a custom class derived from treenode that I want to be able to perform drag'n'drop on to another instance of the same application. I want to serialize the treenode so I implement the ISerializable interface and add a constructor taking a SerializationInfo and a StreamingContext parameter The class essentially looks like this: [Serializable()] public class InheritedTreeNode : TreeNode, ISerializable { bool bool_property1, bool_property2, bool_property3; string string_property4; public InheritedTreeNode() : base() {} ... } here's what the serialization code in the InheritedTreeNode does: public void GetObjectData(SerializationInfo info, StreamingContext context) { // Serialize all properties of this item info.AddValue("bool_property1", typeof(bool)); info.AddValue("bool_property2", typeof(bool)); info.AddValue("bool_property3", typeof(bool)); info.AddValue("string_property4", typeof(string)); // serialize all it's children ArrayList al = new ArrayList(); foreach(InheritedTreeNode itn in this.Nodes) al.Add(mc2); info.AddValue("Nodes", typeof(ArrayList)); } ...and here is how it is Deserialized: public InheritedTreeNode(SerializationInfo info, StreamingContext context) : base() { this.bool_property1 = info.GetBoolean("bool_property1"); this.bool_property2 = info.GetBoolean("bool_property2"); this.bool_property3 = info.GetBoolean("bool_property3"); this.string_property4 = info.GetString("string_property4"); // copy all child nodes ArrayList al = (ArrayList)info.GetValue( "Nodes", typeof(ArrayList) ); foreach(MyTreeNode2 mc2 in al) Nodes.Add(mc2); } now for drag'n'drop i do this: BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.serialize(ms, instance_of_a_tree_node); DataObject dObj = new DataObject("InheritedTreeNode", ms) theTreeView.DoDragDrop( dObj, DragDropEffects.All ); and when dropping in the other tree view the code looks like: IFormatter formatter = new BinaryFormatter(); MemoryStream ms = (MemoryStream)e.Data.GetData( "InheritedTreeNode" ); ms.Position = 0; InheritedTreeNode data = (InheritedTreeNode)formatter.Deserialize(ms); this will throw an exception in the deserialization constructor at the first info.get

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups