修改表单时 InitializeComponent() 上消失的用户控件的实例化

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

大家好,提前谢谢大家。 我创建了一个名为 PanelOption 的用户控件,它是一个带有 CheckBox 和 TextBox 的面板,它们位于 FlowPanelLayOut 中。 它运行良好,一切正常。

当我修改这些PanelOption所在的表单时,问题就来了。它们从 InitializeComponent 方法中消失,如下所示:

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();

在这里您可以看到正在实例化的对象 panelOptionSM1、panelOptionSM2...。但是正如我在设计器上修改表单时所说的那样,Visual Studio 删除了这样的对象的实例化:

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();

有趣的是,无论修改表单如何,与这些对象有关的其余代码都保持不变。我引用的其余代码:

// 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;

还有:

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

这只会在我修改表单时发生,而不会在我重建解决方案或更改表单代码时发生。

您是否手动将这些行添加到 InitializeComponent?您似乎使用参数化构造函数初始化 PanelOption 。如果它没有默认构造函数,那么在不实现自定义 CodeDomSerializer 的情况下,设计人员将不知道如何在修改表单时为此类组件重新生成初始化代码。

最简单的解决方案是向PanelOption添加一个public默认构造函数,并将原始构造函数参数变为public属性。或者,您可以在 InitializeComponent 方法之外手动将这些面板添加到表单中(在这种情况下,它们不会出现在设计器中)。