簡體   English   中英

將控件添加到繼承的Form C#

[英]Add Controls to an inherited Form c#

我創建了兩種形式:

  1. 表格庫
  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.cs [Designer]中調整FormChild的大小時,這兩個按鈕位於FormChild的右下角。

我的問題是,當我在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);

為什么會發生這種情況,如何解決此問題?

謝謝!

一旦在設計視圖中添加控件(無論大小排序)或調整窗體的大小,設計器就會在FormChild.Designer.cs中添加以下行:

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

只需刪除此行,您的表單即可按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM