简体   繁体   English

将控件添加到继承的Form C#

[英]Add Controls to an inherited Form c#

I created two forms: 我创建了两种形式:

  1. FormBase 表格库
  2. FormChild FormChild

FormBase contains a panelMain and two buttons (buttonOk and buttonCancel) which are added to the panelMain. FormBase包含一个panelMain和两个添加到panelMain的按钮(buttonOk和buttonCancel)。

        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. 现在,我想继承FormBase到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. 在FormChild.cs [Designer]中调整FormChild的大小时,这两个按钮位于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. 我的问题是,当我在FormChild.cs [Design]中的panelMain中添加标签,并且现在调整了FormChild的大小时,这两个按钮不停留在FormChild的右下角,而是始终保持默认状态在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: 一旦在设计视图中添加控件(无论大小排序)或调整窗体的大小,设计器就会在FormChild.Designer.cs中添加以下行:

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

Simply delete this line, and your form should work as you expect. 只需删除此行,您的表单即可按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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