Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6089695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:07:45+00:00 2026-05-23T12:07:45+00:00

I have an assembly which contains several UserControl objects that I want to be

  • 0

I have an assembly which contains several UserControl objects that I want to be able to save/load via the application UI. To do this, each control implements the ISerializable interface to customize the fields they need to save.

Here’s a simplified version of that library:

namespace LibraryProject
{
    using System;
    using System.Runtime.Serialization;
    using System.Windows.Forms;

    [Serializable]
    public partial class UserControl1 : UserControl, ISerializable
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public UserControl1(SerializationInfo info, StreamingContext ctxt)
            : this()
        {
            this.checkBox1.Checked = info.GetBoolean("Checked");
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Checked", this.checkBox1.Checked);
        }
    }
}

The client application instantiates several of this controls, and allows the user saving/loading the various UserControl configurations. Here’s a simplified version of the application:

namespace ApplicationProject
{
    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Soap;
    using System.Windows.Forms;
    using LibraryProject;

    public partial class Form1 : Form
    {
        private const string filename = @"test.xml";

        //int hash1;
        //int hash2;

        public Form1()
        {
            InitializeComponent();

            //hash1 = this.ctrl1.GetHashCode();
        }

        private void SaveClick(object sender, EventArgs e)
        {
            using (var stream = File.Open(filename, FileMode.Create))
            {
                var formatter = new SoapFormatter();

                formatter.Serialize(stream, this.ctrl1);
            }
        }

        private void LoadClick(object sender, EventArgs e)
        {
            using (var stream = File.Open(filename, FileMode.Open))
            {
                var formatter = new SoapFormatter();

                this.ctrl1= (UserControl1)formatter.Deserialize(stream);
            }

            //hash2 = this.ctrl1.GetHashCode();
        }
    }
}

On SaveClick, the values are properly saved to file.
On LoadClick, the CheckBox.Checked is properly updated in the Debugger Watch list, but the UI doesn’t reflect the new value.

I have tried adding calls to Refresh(), Invalidate(), Update(), but nothing seems to work.

As expected, hash1 and hash2 are different, but Form1 uses the correct instance.

What am I doing wrong, and how can I fix the UI to display the correct (updated) value?

EDIT: Also, notice that I need to handle multiple config files, that the user must have the ability to save/load to/from a path of her choice

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:07:46+00:00Added an answer on May 23, 2026 at 12:07 pm

    I’m not sure but I’m going to guess it’s because InitializeComponent doesn’t get called.

    But to solve your problem, it is better to serialize a surrogate. Just make little surrogate classes marked [Serializable] and copy properties from the UserControl to the surrogate before serialization. Then you don’t have to mess around with GetObjectData – the serialization process just assumes every property of the surrogate should be serialized.

    The deserialization process will give you a surrogate back. The surrogate just has to know how to instantiate a UserControl and map the properties back to it.

    And if you define a common interface, you don’t have to know which specific type of UserControl you are deserializing:

    var surrogate = formatter.Deserialize(stream) as ISerializationSurrogate;
    UserControl uc = surrogate.Create();
    this.Controls.Add(uc);
    

    Here’s an example of how a surrogate might look:

    [Serializable]
    public class MySurrogate: ISerializationSurrogate
    {
        public MySurrogate() {}
    
        public MySurrogate(MyControl control)
        {
            CB1Checked = control.checkBox1.Checked;
        }
    
        public bool CB1Checked { get; set; }
    
        public Control Create()
        {
            var control = new MyControl();
            control.checkBox1.Checked = CB1Checked;
            return control;
        }
    }
    

    Update

    Actually, I bet the problem is that you’re simply reassigning this.ctrl, which doesn’t change which controls are on the form. What you actually need to do is something like this:

    this.Controls.Remove(existingControl); // if it exists
    this.Controls.Add(newControl);
    

    But you should still use serialization surrogates. They make this kind of stuff much easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A simple question. I have an ASP.NET web application which contains several assemblies and
I have an assembly which should not be used by any application other than
If I have an assembly (A) which references another assembly (B). I want to
I have a .NET project which references another assembly that is built outside of
What I have now (which successfully loads the plug-in) is this: Assembly myDLL =
I have got some code to load an assembly and get all types, which
I have a container DIV which contains several block-DIVS. Every block-DIV contains SPAN items
I have a C# class library that contains several resources files organized in folders.
I'm working with nHibernate. I have make an Assembly (ex : DAL.dll), which contains
In a current (C#) project we have a 3rd party assembly that contains a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.