Rendering a derived user control in a designer view

I have a hierarchy UserControlthat looks something like this:

public class BaseClass : UserControl
{
    protected Label[] Labels;
    public BaseClass(int num)
    {
        Labels = new Label[num];
        for(int i=0; i<num; i++)
        {
            Labels[i] = new Label();
        }
    }
}

And in another file:

public class DerivedClass : BaseClass
{
    public DerivedClass() : base(2)
    {
        // Do stuff to the location, size, font, text of Labels
    }
}

This structure is designed so that BaseClass processes the main logic, and DerivedClass processes the display logic. The number of shortcuts must be variable (different DerivedClasses will have different num values).

, , UserControl, . : -, BaseClass , DerivedClass . , DerivedClass .

. , , , -, . DerivedClass.

+3
2

Windows Forms , , (es) .

:

public partial class BaseControl : UserControl
{
    public BaseControl()
    {
        InitializeComponent();
    }


    protected Label[] Labels;

    public BaseControl(int num) : base()
    { 
        Labels = new Label[num]; 
        for(int i=0; i<num; i++) 
        { 
            Labels[i] = new Label(); 
        } 
    }

}

public class DerivedControl : BaseControl
{

    public DerivedControl() : base(5)
    {
        Controls.Add(Labels[0]);

        Labels[0].Text = "Hello";
        Labels[0].Location = new System.Drawing.Point(10, 10);

    }

}

, Derived Control, . , , DerivedControl:

public class GrandchildControl : DerivedControl
{

    public GrandchildControl() : base() { }

}

, , (Visual Studio 2010), :

GrandchildControl invokes DerivedControl constructor as expected

, . MSDN ( )

  • Form1 , , Form2, . , Form2 Form1, System.Windows.Forms.Form. , , Form2 , Visual Studio Form1 InitializeComponent, .

  • InitializeComponent, . , , , . , , , .

  • ( ) Load , . - InitializeComponent.

, - , , - , InitializeComponent ( , , "" ) , , GrandchildUserControl, , .

FWIW , 1) 3) -, .

1) - Visual Studio Visual Studio . .

+3

, , InitializeComponent . , ( ).

, , , InitializeComponent, , , , . , , , , .

- . :

    protected void CreateLabels(int num)
    {
        Labels = new Label[num];
        for(int i=0; i<num; i++)
        {
            Labels[i] = new Label();
            this.Controls.Add(Labels[i]);
        }
    }

InitializeComponent , num.

InitializeComponent. , , .

, , , InitializeComponent . , . BaseControlCodeDomSerializer, System.ComponentModel.Design.Serialization.CodeDomSerializer, Serialize, :

    public override object Serialize(IDesignerSerializationManager manager, object value)
    {
        BaseControl aCtl = value as BaseControl;
        if (aCtl == null)
            return null;
        if (aCtl.Labels == null)
            return null;
        int num = aCtl.Labels.Length;

        CodeStatementCollection stats = new CodeStatementCollection();

        stats.Add(new CodeSnippetExpression("CreateLabels(" + num.ToString() + ")"));

        return stats;
    }

, Serializer :

[DesignerSerializer("MyControls.BaseControlCodeDomSerializer", typeof(CodeDomSerializer))]
+2

All Articles