Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how i can , Form the [Serializable()] ??

i want use of BinaryFormatter.
Posted
Updated 16-Feb-11 0:23am
v3
Comments
Sandeep Mewara 16-Feb-11 5:49am    
Not clear. Elaborate.
jim lahey 16-Feb-11 5:52am    
Do you want to serialize your form?
Michael Waguih 16-Feb-11 5:55am    
Please can you improve your question
Albin Abel 16-Feb-11 6:04am    
it is possible, you have implement the Iserializable interface in all the controls and form. In that way you need to use custom controls for all the form controls. However not sure the whole form can be serializable...but most of the controls in it once implemented Iserializable then it can be serialized. Not sure @ complex controls like grid view n all. But why a form need to be serialized.

I understand, you want to store form status (sizes, selected values, position of focus, entered data, etc.) to be able to restore all that later, right.

You can solve this problem, but you should not try it with the form itself — bad idea. That is messing up UI and data aspects together. Don't do it, you will sunk in problems!

Better create separate pure-data class, make serialization for this class only, store/load instances of this class and semantically map in from/to your form. What you need to persist on the form will never be universal anyway, all essential data will be semantic.

—SA
 
Share this answer
 
Comments
[no name] 16-Feb-11 13:09pm    
ok.but i want know where ,it possible where i can serialized the form(class)
Sergey Alexandrovich Kryukov 16-Feb-11 13:27pm    
Yes, it is possible.
--SA
Espen Harlinn 16-Feb-11 14:29pm    
Good proposal, my 5
Sergey Alexandrovich Kryukov 16-Feb-11 15:26pm    
Thank you, Espen.
--SA
You would like to do something like this I suppose:
C#
[Serializable]
public class Element
{
 public int field1;
 public string field2;

 public Element()
 {
 }

 public Element(int f1, string f2)
 {
   field1 = f1;
   field2 = f2;
 } 
}

static void Serialize() 
{

  FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
  List<element> elements = new List<element>();
  
  elements.Add(new Element(1,"One"));
  elements.Add(new Element(2,"Two"));

  // Construct a BinaryFormatter and use it to 
  // serialize the data to the stream.
  BinaryFormatter formatter = new BinaryFormatter();
  try 
  {
    formatter.Serialize(fs, elements);
  }
  catch (SerializationException e) 
  {
    Console.WriteLine("Failed to serialize. Reason: " + e.Message);
    throw;
  }
  finally 
  {
    fs.Close();
  }
}


Obviously you will have to create something better than my Element class, but if your create similar classes reflecting the properties of the form you can then bind to the elements through a BindingSource. I think this is basically what SAKryukov suggested :)

Regards
Espen Harlinn
 
Share this answer
 
v7
Comments
Sergey Alexandrovich Kryukov 16-Feb-11 13:32pm    
Espen, I had trouble to fix you formatting. You should use < and > character entitied instead of angular brackets to escape HTML formatting; not doing so created parasitic <element> tags at the end. Do you mind my fix?
--SA
Espen Harlinn 16-Feb-11 14:21pm    
No - super, thanks SAKryukov!
Sergey Alexandrovich Kryukov 17-Feb-11 19:27pm    
Now I can read it... Well, my 5.
You also avoid serialization of the form, which I think is the best/
Oh, by the way, this is a good application to advice DataContract, as all persistence can be introduced from scratch. Don't you think so?
--SA
Not clear with your question. As of my knowledge this[^]link might help you to solve your problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900