2016-08-24 52 views
0

我创建两种形式:控件添加到一种遗传形式C#

  1. FormBase
  2. FormChild

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; 

现在我想继承FormBase到FormChild。

public partial class FormChild : FormBase 

当FormChild在FormChild.cs调整大小[设计师]两个按钮留在FormChild的右下端。

我的问题是,当我在FormChild.cs [设计]和FormChild标签添加到panelMain现在的大小时,两个按钮FormChild的右下端鸵鸟政策住宿,而不是它们始终保持在FormBase中定义的默认位置。

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

为什么会发生这种情况,我该如何解决这个问题?

谢谢!

+0

锚在继承方案中效果不佳,控件锚定到原始客户端大小,而不是新大小。 SuspendLayout()影响了多少对我来说不是很清楚,但是无论如何你无法做任何事情。我认为*唯一实际的解决方案是在派生类中应用锚,或者在Load事件触发之前不更改大小。 –

+0

非常感谢您的回答!然后我会尝试找到另一种方式。 –

回答

0

只要你添加控件(没关系什么样)或调整在设计视图的形式,设计器添加以下行FormChild.Designer.cs:

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

干脆删除这条线和你的表格应该按照你的预期工作。