1

I have created a control with controlcollection. When I add items from the property window at design time. It added perfectly. Also when I open it back. Added items shows me. But, When I close the form then open it again the items was removed.

Now I have added two Items in the collection. The items was looking perfectly. enter image description here

But, When I open the Form.Desigern.cs file the following line is missing.

this.xWizardControl.Window.Controls.Add(this.xWizardPage1);
this.xWizardControl.Window.Controls.Add(this.xWizardPage2);

enter image description here

The code is looks like this.

public class XWizardPageWindow : DevExpress.XtraEditors.XtraUserControl, ISupportInitialize
{
    private XWizardPageCollection _pages;
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public XWizardPageCollection Pages
    {
        get { return _pages; }
        set { _pages = value; }
    }
    public XWizardPageWindow()
    {
    }
    #region Override Methods
    protected override ControlCollection CreateControlsInstance()
    {
        if (_pages == null)
            _pages = new XWizardPageCollection(this);
        return _pages;
    }
    #endregion


    #region ISupportInitialize Members

    public void BeginInit()
    {
        //DO NOTHING
    }

    public void EndInit()
    {
        //DO NOTHING
    }

    #endregion
}

ControlCollection Class

public class XWizardPageCollection : System.Windows.Forms.Control.ControlCollection
{
    public delegate void XWizardPageEventHandler(object sender, XWizardPageEventArgs e);
    List<XWizardPage> _pages = new List<XWizardPage>();
    #region Constructor
    public XWizardPageCollection(System.Windows.Forms.Control owner): base(owner)
    {}
    #endregion

    #region Override Methods
    public override void Add(System.Windows.Forms.Control value)
    {
        base.Add(value);
        value.Dock = System.Windows.Forms.DockStyle.Fill;
        ((XWizardPage)value).BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
    }
    #endregion

    #region Destructor
    ~XWizardPageCollection()
    {
        GC.SuppressFinalize(this);
    }
    #endregion
}
  • May I answer in `vb.net`? – Bjørn-Roger Kringsjå Mar 08 '14 at 12:41
  • Sure, I can understand both languages. –  Mar 08 '14 at 17:38
  • How do you add the control to the collection? Using the shown control collection dialog? You see, I couldn't reproduce this behavior. However, you could try to change the `DesignerSerializationVisibilityAttribute` from `Visible` to `Content`. If no success, I have some recommendations. First, one should never change the `ControlCollection` after it's first created. So the `Pages` property should be decleared `ReadOnly`. With that being said, if you want to do this as the `TabControl` does it, then `Pages` property should be a wrapped list reflecting the controls collection. – Bjørn-Roger Kringsjå Mar 08 '14 at 18:03
  • Yes, the controls will be added from the control collection dialog shown in the screen shot. `XWizardPageWindow` will contains `XWizardPages` class object and `XWizardPageWindow` will be added on `XWizardControl` client area. –  Mar 10 '14 at 04:11

1 Answers1

1

First, one should never change the ControlCollection once created and returned by CreateControlsInstance. So the Pages property should be defined as ReadOnly.

Secondly, when using Visible you're telling the code generator to create a new instance of Pages, which we don't want. So change the DesignerSerializationVisibilityAttribute from Visible to Content and the code generator will produce code for the contents of the object (Pages), rather than for the object itself.

Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64