locked
how to make the treeview serialisable RRS feed

  • Question


  • hello


    i tried to serialize the treeview but i am getting an error like this 

    ype 'System.Windows.Forms.TreeView' in Assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.


    so how can we explicitly make the treeview serialisable i have tried [serialisable] on top of the class then also it is getting


    please replay me


    Thanks&Regards

    Ginnas
    Monday, January 14, 2013 12:36 PM

Answers

  • Dear ginnas,

    how could you marked [serialisable] attribute to a predefined class? That is npt possible, since you can not change the code of predefined classes.


    One good question is equivalent to ten best answers.

    • Proposed as answer by Mike Feng Tuesday, January 15, 2013 5:44 AM
    • Marked as answer by Mike Feng Tuesday, January 22, 2013 10:52 AM
    Monday, January 14, 2013 3:38 PM
  • Create a new control that inherits from TreeView and implement ISerializable.

    Your serialzation routine will need to manually record property values of the TreeView and then will need to recursively walk the nodes collection, adding enough information to the stream to be able to recreate the treenodes.

    Then your deserialization routine would create a new TreeView instance, set its properties according to the values you saved, and then load all of its tree nodes (create new TreeNodes and setting their properties) according to the info you saved when walking the nodes collection.

    The specific information you write to the stream will depend on how your TreeView is being used.  You'll need to save as much information as it takes to recreate the TreeView from a new instance of the control.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Mike Feng Tuesday, January 15, 2013 5:44 AM
    • Marked as answer by Mike Feng Tuesday, January 22, 2013 10:52 AM
    Monday, January 14, 2013 3:47 PM

All replies

  • Dear ginnas,

    how could you marked [serialisable] attribute to a predefined class? That is npt possible, since you can not change the code of predefined classes.


    One good question is equivalent to ten best answers.

    • Proposed as answer by Mike Feng Tuesday, January 15, 2013 5:44 AM
    • Marked as answer by Mike Feng Tuesday, January 22, 2013 10:52 AM
    Monday, January 14, 2013 3:38 PM
  • Create a new control that inherits from TreeView and implement ISerializable.

    Your serialzation routine will need to manually record property values of the TreeView and then will need to recursively walk the nodes collection, adding enough information to the stream to be able to recreate the treenodes.

    Then your deserialization routine would create a new TreeView instance, set its properties according to the values you saved, and then load all of its tree nodes (create new TreeNodes and setting their properties) according to the info you saved when walking the nodes collection.

    The specific information you write to the stream will depend on how your TreeView is being used.  You'll need to save as much information as it takes to recreate the TreeView from a new instance of the control.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Mike Feng Tuesday, January 15, 2013 5:44 AM
    • Marked as answer by Mike Feng Tuesday, January 22, 2013 10:52 AM
    Monday, January 14, 2013 3:47 PM