保持面板上的按钮位置调整大小

时间:2012-07-06 20:49:38

标签: c# .net winforms panel

我一直在搞乱一些应该简单的事情。

我已经转移了工作并试图获得我之前使用过的一些基本工具,但当然我没有旧的资源来看待。

我们扩展了面板以具有一些标准属性和功能(保存,关闭,保存和关闭)。

但我无法在调整大小时正确定位按钮。我把这个ExtPanel放在一个表单上,但是当我调整大小时按钮不断消失,或者没有按预期移动(在右下角冻结)。

班级

public partial class ExtPanel: UserControl
   {
   private System.Windows.Forms.Button btnSaveandClose;
   private System.Windows.Forms.Button btnCancel;
   private System.Windows.Forms.Button btnSave;

   public ExtPanel ()
      {
      InitializeComponent ();
      }

// misc things this class does...


}

public partial class ExtPanel
    {
    private void InitializeComponent ()
       {
        this.btnSaveandClose = new System.Windows.Forms.Button();
        this.btnCancel = new System.Windows.Forms.Button();
        this.btnSave = new System.Windows.Forms.Button();
        this.panel1 = new System.Windows.Forms.Panel();
        this.panel1.SuspendLayout();
        this.SuspendLayout();
        // 
        // btnSaveandClose
        // 
        this.btnSaveandClose.Location = new System.Drawing.Point(899, 689);
        this.btnSaveandClose.Name = "btnSaveandClose";
        this.btnSaveandClose.Size = new System.Drawing.Size(100, 30);
        this.btnSaveandClose.TabIndex = 0;
        this.btnSaveandClose.Text = "Save and Close";
        this.btnSaveandClose.UseVisualStyleBackColor = true;
        this.btnSaveandClose.Click += new System.EventHandler(this.Click_SaveandClose);
        this.btnSaveandClose.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));

        // 
        // btnCancel
        // 
        this.btnCancel.Location = new System.Drawing.Point(687, 689);
        this.btnCancel.Name = "btnCancel";
        this.btnCancel.Size = new System.Drawing.Size(100, 30);
        this.btnCancel.TabIndex = 1;
        this.btnCancel.Text = "Cancel";
        this.btnCancel.UseVisualStyleBackColor = true;
        this.btnCancel.Click += new System.EventHandler(this.Click_Close);
        this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));

        // 
        // btnSave
        // 
        this.btnSave.Location = new System.Drawing.Point(793, 689);
        this.btnSave.Name = "btnSave";
        this.btnSave.Size = new System.Drawing.Size(100, 30);
        this.btnSave.TabIndex = 2;
        this.btnSave.Text = "Save";
        this.btnSave.UseVisualStyleBackColor = true;
        this.btnSave.Click += new System.EventHandler(this.Click_Save);
        this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));

        // 
        // panel1
        // 
        this.panel1.AutoScroll = true;
        this.panel1.Controls.Add(this.btnSave);
        this.panel1.Controls.Add(this.btnCancel);
        this.panel1.Controls.Add(this.btnSaveandClose);
        this.panel1.Location = new System.Drawing.Point(0, 0);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(1008, 730);
        this.panel1.TabIndex = 0;
        // 
        // ExtPanel
        // 
        this.Controls.Add(this.panel1);
        this.Name = "ExtPanel";
        this.Size = this.panel1.Size;
        this.Click += new System.EventHandler(this.click_this);
        this.panel1.ResumeLayout(false);
        this.ResumeLayout(false);

        }

    #endregion

    private System.Windows.Forms.Panel panel1;


}

1 个答案:

答案 0 :(得分:7)

您是否尝试过锚定按钮?

......我错过了锚定代码。

如果在调整表单大小时右下角有一个按钮要保留在右下角,请将其锚点属性设置为Bottom,Right。

<强>更新
我加载了你的代码。您在ExtPanel内有一个面板。如果您停靠(Fill)该面板,那么您应该通过调整ExtPanel的大小来正常工作。