简体   繁体   中英

Add Controls to an inherited Form c#

I created two forms:

  1. FormBase
  2. FormChild

FormBase contains a panelMain and two buttons (buttonOk and buttonCancel) which are added to the panelMain.

        private void InitializeComponent()
    {
        this.buttonCancel = new System.Windows.Forms.Button();
        this.buttonOk = new System.Windows.Forms.Button();
        this.panelMain = new System.Windows.Forms.Panel();
        this.panelMain.SuspendLayout();
        this.SuspendLayout();
        // 
        // buttonCancel
        // 
        this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.buttonCancel.Location = new System.Drawing.Point(465, 208);
        this.buttonCancel.Name = "buttonCancel";
        this.buttonCancel.Size = new System.Drawing.Size(107, 42);
        this.buttonCancel.TabIndex = 0;
        this.buttonCancel.Text = "Cancel";
        this.buttonCancel.UseVisualStyleBackColor = true;
        // 
        // buttonOk
        // 
        this.buttonOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.buttonOk.Location = new System.Drawing.Point(352, 208);
        this.buttonOk.Name = "buttonOk";
        this.buttonOk.Size = new System.Drawing.Size(107, 42);
        this.buttonOk.TabIndex = 1;
        this.buttonOk.Text = "Ok";
        this.buttonOk.UseVisualStyleBackColor = true;
        // 
        // panelMain
        // 
        this.panelMain.Controls.Add(this.buttonOk);
        this.panelMain.Controls.Add(this.buttonCancel);
        this.panelMain.Dock = System.Windows.Forms.DockStyle.Fill;
        this.panelMain.Location = new System.Drawing.Point(0, 0);
        this.panelMain.Name = "panelMain";
        this.panelMain.Size = new System.Drawing.Size(584, 262);
        this.panelMain.TabIndex = 2;
        // 
        // FormBase
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(584, 262);
        this.Controls.Add(this.panelMain);
        this.Name = "FormBase";
        this.Text = "FormBase";
        this.panelMain.ResumeLayout(false);
        this.ResumeLayout(false);

    }

    #endregion

    internal System.Windows.Forms.Button buttonCancel;
    internal System.Windows.Forms.Button buttonOk;
    public System.Windows.Forms.Panel panelMain;

Now I want to inherit FormBase to FormChild.

    public partial class FormChild : FormBase

When the FormChild is resized in the FormChild.cs[Designer] the two buttons stay at the bottom-right end of FormChild.

My problem is that when I add a label to the panelMain in the FormChild.cs[Design] and the FormChild is resized now, the two buttons don´t stay at the bottom-right end of FormChild, instead they always remain at the default location defined in FormBase.

this.buttonCancel.Location = new System.Drawing.Point(465, 208);
this.buttonOk.Location = new System.Drawing.Point(352, 208);

Why is this happening and how can I fix this problem?

Thanks!

As soon as you add a control (it does not matter what sort) or resize the form in design view, the Designer adds the following line to the FormChild.Designer.cs:

this.panelMain.Size = new System.Drawing.Size(xxx, yyy);

Simply delete this line, and your form should work as you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM