Udostępnij za pośrednictwem


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.

Example

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. Also see Walkthrough: Serializing Collections of Standard Types with the DesignerSerializationVisibilityAttribute.

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

' This sample demonstrates the use of the  
' DesignerSerializationVisibility attribute 
' to serialize a collection of strings 
' at design time. 
Class Form1
   Inherits Form
   Private serializationDemoControl1 As SerializationDemoControl


   Public Sub New()
      InitializeComponent()
    End Sub 


   ' 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 Sub InitializeComponent()
        Me.serializationDemoControl1 = New SerializationDemoControl
        Me.SuspendLayout()
        ' 
        'serializationDemoControl1 
        ' 
        Me.serializationDemoControl1.Location = New System.Drawing.Point(0, 0)
        Me.serializationDemoControl1.Name = "serializationDemoControl1" 
        Me.serializationDemoControl1.Padding = New System.Windows.Forms.Padding(5)
        Me.serializationDemoControl1.TabIndex = 0
        ' 
        'Form1 
        ' 
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.serializationDemoControl1)
        Me.Name = "Form1" 
        Me.Text = "Form1" 
        Me.ResumeLayout(False)

    End Sub


   <STAThread()>  _
   Shared Sub Main()
      Application.EnableVisualStyles()
      Application.Run(New Form1())
    End Sub 
End Class 

Public Class SerializationDemoControl
   Inherits UserControl
   ' This is the TextBox contained by  
   ' the SerializationDemoControl. 
   Private textBox1 As System.Windows.Forms.TextBox

   ' This field backs the Strings property. 
    Private stringsValue(1) As String 

    Public Sub New()
        InitializeComponent()
    End Sub 

   ' 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.
    <DesignerSerializationVisibility( _
        DesignerSerializationVisibility.Content)> _
    Public Property Strings() As String()
        Get 
            Return Me.stringsValue
        End Get 
        Set(ByVal value As String())
            Me.stringsValue = Value

            ' Populate the contained TextBox with the values 
            ' in the stringsValue array. 
            Dim sb As New StringBuilder(Me.stringsValue.Length)

            Dim i As Integer 
            For i = 0 To (Me.stringsValue.Length) - 1
                sb.Append(Me.stringsValue(i))
                sb.Append(ControlChars.Cr + ControlChars.Lf)
            Next i

            Me.textBox1.Text = sb.ToString()
        End Set 
    End Property 

   Private Sub InitializeComponent()
      Me.textBox1 = New System.Windows.Forms.TextBox()
      Me.SuspendLayout()

      ' Settings for the contained TextBox control. 
      Me.textBox1.AutoSize = False 
      Me.textBox1.Dock = System.Windows.Forms.DockStyle.Fill
      Me.textBox1.Location = New System.Drawing.Point(5, 5)
      Me.textBox1.Margin = New System.Windows.Forms.Padding(0)
      Me.textBox1.Multiline = True 
      Me.textBox1.Name = "textBox1" 
      Me.textBox1.ReadOnly = True 
      Me.textBox1.ScrollBars = ScrollBars.Vertical
      Me.textBox1.Size = New System.Drawing.Size(140, 140)
      Me.textBox1.TabIndex = 0

      ' Settings for SerializationDemoControl. 
      Me.Controls.Add(textBox1)
      Me.Name = "SerializationDemoControl" 
      Me.Padding = New System.Windows.Forms.Padding(5)
      Me.ResumeLayout(False)
    End Sub 
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

// This sample demonstrates the use of the  
// DesignerSerializationVisibility attribute 
// to serialize a collection of strings 
// at design time. 
namespace SerializationDemo
{
    class Form1 : Form
    {
        private SerializationDemoControl serializationDemoControl1;

        public Form1()
        {
            InitializeComponent();            
        }

        // 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.serializationDemoControl1 = new SerializationDemo.SerializationDemoControl();
            this.SuspendLayout();
            //  
            // serializationDemoControl1 
            //  
            this.serializationDemoControl1.Location = new System.Drawing.Point(0, 0);
            this.serializationDemoControl1.Name = "serializationDemoControl1";
            this.serializationDemoControl1.Padding = new System.Windows.Forms.Padding(5);
            this.serializationDemoControl1.TabIndex = 0;
            //  
            // Form1 
            //  
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.serializationDemoControl1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }

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

        // This field backs the Strings property. 
        private String[] stringsValue = new String[1];

        public SerializationDemoControl()
        {
            InitializeComponent();
        }

        // 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.
        [DesignerSerializationVisibility( 
            DesignerSerializationVisibility.Content )]
        public String[] Strings
        {
            get
            {
                return this.stringsValue;
            }
            set
            {
                this.stringsValue = value;

                // Populate the contained TextBox with the values 
                // in the stringsValue array.
                StringBuilder sb = 
                    new StringBuilder(this.stringsValue.Length);

                for (int i = 0; i < this.stringsValue.Length; i++)
                {
                    sb.Append(this.stringsValue[i]);
                    sb.Append("\r\n");
                }

                this.textBox1.Text = sb.ToString();
            }
        }

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();

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

            // Settings for SerializationDemoControl. 
            this.Controls.Add(this.textBox1);
            this.Name = "SerializationDemoControl";
            this.Padding = new System.Windows.Forms.Padding(5);
            this.ResumeLayout(false);
        }
    }
}
#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());
}

See Also

Reference

DesignerSerializationVisibilityAttribute

Concepts

Designer Serialization Overview

Other Resources

Extending Design-Time Support