This documentation is archived and is not being maintained.

How to: Serialize Collections of Standard Types with the DesignerSerializationVisibilityAttribute

When your custom controls expose a collection as a property, you can serialize the collection at design time.

This example demonstrates how to use the DesignerSerializationVisibilityAttribute class to control how a collection is serialized at design time. Applying the Content value to your collection property ensures that the property will be serialized.

There is extensive support for this task in Visual Studio. Walkthrough: Serializing Collections of Standard Types with the DesignerSerializationVisibilityAttribute
Walkthrough: Serializing Collections of Standard Types with the DesignerSerializationVisibilityAttribute
Walkthrough: Serializing Collections of Standard Types with the DesignerSerializationVisibilityAttribute
Walkthrough: Serializing Collections of Standard Types with the DesignerSerializationVisibilityAttribute

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Collections::Generic;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Text;
using namespace System::Windows::Forms;

// This sample demonstrates the use of the  
// DesignerSerializationVisibility attribute 
// to serialize a collection of strings 
// at design time. 
namespace SerializationDemo
{

    public ref class SerializationDemoControl : 
        public System::Windows::Forms::UserControl
    {
        // This is the TextBox contained by  
        // the SerializationDemoControl. 
    private:
        System::Windows::Forms::TextBox^ demoControlTextBox;

        // This field backs the Strings property. 
    private:
        array<String^>^ stringsValue;



    public:
        SerializationDemoControl()
        {
            InitializeComponent();
            stringsValue = gcnew array<String^>(1);
        }

        // When the DesignerSerializationVisibility attribute has 
        // a value of "Content" or "Visible" the designer will 
        // serialize the property. This property can also be edited  
        // at design time with a CollectionEditor. 
    public:
        [DesignerSerializationVisibility(
            DesignerSerializationVisibility::Content)]
        property array<String^>^ Strings
        {
            array<String^>^ get()
            {
                return this->stringsValue;
            }
            void set(array<String^>^ value)
            {
                this->stringsValue = value;

                // Populate the contained TextBox with the values 
                // in the stringsValue array.
                StringBuilder^ sb =
                    gcnew StringBuilder(this->stringsValue->Length);

                for (int i = 0; i < this->stringsValue->Length; i++)
                {
                    sb->Append(this->stringsValue[i]);
                    sb->Append(Environment::NewLine);
                }

                this->demoControlTextBox->Text = sb->ToString();
            }
        }

    private:
        void InitializeComponent()
        {
            this->demoControlTextBox = 
                gcnew System::Windows::Forms::TextBox();
            this->SuspendLayout();

            // Settings for the contained TextBox control. 
            this->demoControlTextBox->AutoSize = false;
            this->demoControlTextBox->Dock = 
                System::Windows::Forms::DockStyle::Fill;
            this->demoControlTextBox->Location = 
                System::Drawing::Point(5, 5);
            this->demoControlTextBox->Margin =
                System::Windows::Forms::Padding(0);
            this->demoControlTextBox->Multiline = true;
            this->demoControlTextBox->Name = "textBox1";
            this->demoControlTextBox->ReadOnly = true;
            this->demoControlTextBox->ScrollBars = ScrollBars::Vertical;
            this->demoControlTextBox->Size = 
                System::Drawing::Size(140, 140);
            this->demoControlTextBox->TabIndex = 0;

            // Settings for SerializationDemoControl. 
            this->Controls->Add(this->demoControlTextBox);
            this->Name = "SerializationDemoControl";
            this->Padding = System::Windows::Forms::Padding(5);
            this->ResumeLayout(false);
        }
    };

    public ref class SerializationDemoForm : 
        public System::Windows::Forms::Form
    {

        SerializationDemoControl^ serializationDemoControl;


    public:
        SerializationDemoForm()
        {
            InitializeComponent();
            serializationDemoControl = nullptr;
        }

        // The Windows Forms Designer emits code to this method.  
        // If an instance of SerializationDemoControl is added  
        // to the form, the Strings will be serialized here. 
    private:
        void InitializeComponent()
        {
            this->serializationDemoControl =
                gcnew SerializationDemo::SerializationDemoControl();
            this->SuspendLayout();
            //  
            // serializationDemoControl 
            //  
            this->serializationDemoControl->Location =
                System::Drawing::Point(0, 0);
            this->serializationDemoControl->Name = 
                "serializationDemoControl";
            this->serializationDemoControl->Padding =
                System::Windows::Forms::Padding(5);
            this->serializationDemoControl->TabIndex = 0;
            //  
            // SerializationDemoForm 
            //  
            this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
            this->ClientSize = System::Drawing::Size(292, 273);
            this->Controls->Add(this->serializationDemoControl);
            this->Name = "SerializationDemoForm";
            this->Text = "Form1";
            this->ResumeLayout(false);

        }
    };
};

[STAThread]
int main()
{
    Application::EnableVisualStyles();
    Application::Run(gcnew SerializationDemo::SerializationDemoForm());
}
Show: