Question

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
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top