'Instantiation of User Controls dissapearing on InitalizeComponent() when modifiyng Form

Hi everyone and thank you in advance. I have created a User Control named PanelOption that is a panel with a CheckBox and a TextBox, and these are inside a FlowPanelLayOut. It runs well and everything works perfectly.

The problem comes when I modify the form where these PanelOption are. They dissapear from the InitializeComponent method as I show you below:

private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CrearMultipleForm));
        this.txtEnunciado = new System.Windows.Forms.TextBox();
        this.txtPuntaje = new System.Windows.Forms.TextBox();
        this.lblPuntaje = new System.Windows.Forms.Label();
        this.lbNumPreg = new System.Windows.Forms.Label();
        this.FlowPanelOpciones = new System.Windows.Forms.FlowLayoutPanel();
        this.panelOptionSM1 = new Custom_Components.PanelOption('A');
        this.panelOptionSM2 = new Custom_Components.PanelOption('C');
        this.panelOptionSM3 = new Custom_Components.PanelOption('B');
        this.panelOptionSM4 = new Custom_Components.PanelOption('D');
        this.panel2 = new System.Windows.Forms.Panel();
        this.botonMinus = new QuizifyIU.Controles_Personalizados.Boton();
        this.botonPlus = new QuizifyIU.Controles_Personalizados.Boton();
        this.btnCrear = new QuizifyIU.Controles_Personalizados.Boton();
        this.btnCancelar = new QuizifyIU.Controles_Personalizados.Boton();
        this.btnRespAbierta = new QuizifyIU.Controles_Personalizados.Boton();
        this.btnVF = new QuizifyIU.Controles_Personalizados.Boton();
        this.btnMultiple = new QuizifyIU.Controles_Personalizados.Boton();
        this.flowLayoutPanel1.SuspendLayout();
        this.panel1.SuspendLayout();
        this.FlowPanelOpciones.SuspendLayout();
        this.panel2.SuspendLayout();
        this.SuspendLayout();

Here you can see the objects panelOptionSM1, panelOptionSM2... being instantiated . But as I said when I modify the form on the designer, Visual Studio removes the instantiation of the objects like this:

private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CrearMultipleForm));
        this.txtEnunciado = new System.Windows.Forms.TextBox();
        this.txtPuntaje = new System.Windows.Forms.TextBox();
        this.lblPuntaje = new System.Windows.Forms.Label();
        this.lbNumPreg = new System.Windows.Forms.Label();
        this.FlowPanelOpciones = new System.Windows.Forms.FlowLayoutPanel();
        this.panel2 = new System.Windows.Forms.Panel();
        this.botonMinus = new QuizifyIU.Controles_Personalizados.Boton();
        this.botonPlus = new QuizifyIU.Controles_Personalizados.Boton();
        this.btnCrear = new QuizifyIU.Controles_Personalizados.Boton();
        this.btnCancelar = new QuizifyIU.Controles_Personalizados.Boton();
        this.btnRespAbierta = new QuizifyIU.Controles_Personalizados.Boton();
        this.btnVF = new QuizifyIU.Controles_Personalizados.Boton();
        this.btnMultiple = new QuizifyIU.Controles_Personalizados.Boton();
        this.flowLayoutPanel1.SuspendLayout();
        this.panel1.SuspendLayout();
        this.FlowPanelOpciones.SuspendLayout();
        this.panel2.SuspendLayout();
        this.SuspendLayout();

The funny part is that the rest of the code concerning these objects remains untouched regardless of modifying the form. The rest of the code I am refereing:

// panelOpcionSM1
        // 
        this.panelOptionSM1.BackColor = System.Drawing.Color.Transparent;
        this.panelOptionSM1.Location = new System.Drawing.Point(3, 3);
        this.panelOptionSM1.Name = "panelOpcionSM1";
        this.panelOptionSM1.Size = new System.Drawing.Size(520, 48);
        this.panelOptionSM1.TabIndex = 110;
        // 
        // panelOpcionSM4
        // 
        this.panelOptionSM4.BackColor = System.Drawing.Color.Transparent;
        this.panelOptionSM4.Location = new System.Drawing.Point(3, 57);
        this.panelOptionSM4.Name = "panelOpcionSM4";
        this.panelOptionSM4.Size = new System.Drawing.Size(520, 48);
        this.panelOptionSM4.TabIndex = 112;
        // 
        // panelOpcionSM2
        // 
        this.panelOptionSM2.BackColor = System.Drawing.Color.Transparent;
        this.panelOptionSM2.Location = new System.Drawing.Point(3, 111);
        this.panelOptionSM2.Name = "panelOpcionSM2";
        this.panelOptionSM2.Size = new System.Drawing.Size(520, 48);
        this.panelOptionSM2.TabIndex = 111;
        // 
        // panelOpcionSM3
        // 
        this.panelOptionSM3.BackColor = System.Drawing.Color.Transparent;
        this.panelOptionSM3.Location = new System.Drawing.Point(3, 165);
        this.panelOptionSM3.Name = "panelOpcionSM3";
        this.panelOptionSM3.Size = new System.Drawing.Size(525, 48);
        this.panelOptionSM3.TabIndex = 109;

And also:

private Custom_Components.PanelOption panelOptionSM3;
    private Custom_Components.PanelOption panelOptionSM1;
    private Custom_Components.PanelOption panelOptionSM4;
    private Custom_Components.PanelOption panelOptionSM2;

This only happens when I modify the form, not when I rebuild the solution or change the code of the form.



Solution 1:[1]

Did you add those lines manually to InitializeComponent? It seems you initialize PanelOption with a parameterized constructor. If it has no default constructor, then without implementing a custom CodeDomSerializer the designer will not have a clue how to regenerate the initialization code for such a component when you modify the form.

The simplest solution is to add a public default constructor to PanelOption and turn the original constructor parameter to a public property. Alternatively, you can add those panels to the form manually, outside of the InitializeComponent method (in which case they will not appear in the designer).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 György K?szeg