2016-08-24 43 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); 

乾脆刪除這條線和你的表格應該按照你的預期工作。

相關問題