简体   繁体   中英

How to serialize and deserialize tabcontrol c#

I am using a tabcontrol which I want to serialize and save . I am using this code but it gives that tabcontrol class is not marked as serializable . How to mark it serializable as I am not able to override the class . How to do it ?

using (Stream stream = File.Open("data.dat", FileMode.Create))
{
    BinaryFormatter bin = new BinaryFormatter();
    bin.Serialize(stream,tabControl1);
}

It gives this error

System.Windows.Forms.TabControl not marked as serializable

Why don't serialize Controls and is there an alternative?

If you serialize a control, there are some problems:

  1. You can't do it because System.Windows.Forms.TabControl not marked as serializable like you have seen.
  2. If you will do it and only if it is allowed, are there a lot of properties and classes, interfaces, events etc. that are serialized with it, inherited from the classes above and that is not what you will.

    在此处输入图片说明

The only way you could do it is by made a new class, bind all the values you will save with the properties and serialize that class.

[Serializable] // don't forget this! It will mark your class so you can serialize it.
public class BindingClass // p.s.: give this a better name!
{
    public string Text { get; set; } // Bind whit a control of your tab control.
    public float Number { get; set; }
    public string ImageLocation { get; set; } // used for the image
    public IEnumerable<object> ListOfString { get; set; } // used for a list
}

Code example

Text and numbers

Well for text and numbers it is easy. You can made an intense of your class and you can bind that. After it you can serialize it. An example:

BindingClass bc = new BindingClass();
bc.Text = textBox1.Text;
bc.Number = numbericUpDown.Value;

using (Stream stream = File.Open("data.dat", FileMode.Create))
{
    BinaryFormatter bin = new BinaryFormatter();
    bin.Serialize(stream, bc);
}

Images

For images is it a little bit complex. You can serialize an image but it is also a bad thing to do that. Better is to save the image in your bin/debug folder of your project and serialize the path of that image. An example:

string imageLocation = Application.StartupPath + @"\myImage.jpg"
pictureBox1.Image.Save(imageLocation, ImageFormat.Jpeg);

// declare bc like code above.
bc.ImageLocation = imageLocation;

// serialize bc.

If the image already exists in the file, you can override it. But if you will work with histories, not a good thing... You can solve it by use the current date time as filename! Change your code with this:

string imageLocation = Application.StartupPath +
                       DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"

Note: You can also use a blob service like Azure and Amazon (not free) or upload images to Imgur , Flickr or 9gag (patical free) . Remark that there must be an internet connection between the client and server. You can upload by searching on Google how to do it.

List of string

For list of strings you can use this:

bc.ListOfString = comboBox1.Items;

Note

I haven't test the code. So if you have a problem with one the examples comment it and I will look at it, but try also to look on Google for a solution for your problem. Try it yourself, best way to learn...

Alternative for serialize (update 16-Jun-16)

Serializing is a save way to make your code unreadable for people. However that can give problems if you scale your application. The problem is also happen by Microsoft Word. The old .doc files are also serialized code, the new .docx files are zipped xml files and now it's easier to make .docx files.

Good alternatives are Json or XML .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM