2

我在一个面板中包含了 4 个按钮。面板停靠在主窗口上。

当我调整主窗口的大小时,它不会根据新修改的窗口大小重新定位 4 个按钮。我正在使用 VS 2010 Designer 视图来完成此操作。

这是从 Designer.cs 生成的完整代码

private void InitializeComponent()
{
    this.statusStrip1 = new System.Windows.Forms.StatusStrip();
    this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
    this.TreeDDC = new System.Windows.Forms.TreeView();
    this.BtnPointCtrl = new System.Windows.Forms.Button();
    this.BtnLogic = new System.Windows.Forms.Button();
    this.BtnComm = new System.Windows.Forms.Button();
    this.BtnSystem = new System.Windows.Forms.Button();
    this.panel1 = new System.Windows.Forms.Panel();
    this.statusStrip1.SuspendLayout();
    this.panel1.SuspendLayout();
    this.SuspendLayout();
    // 
    // statusStrip1
    // 
    this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
    this.toolStripStatusLabel1});
    this.statusStrip1.Location = new System.Drawing.Point(0, 445);
    this.statusStrip1.Name = "statusStrip1";
    this.statusStrip1.Size = new System.Drawing.Size(639, 22);
    this.statusStrip1.TabIndex = 0;
    this.statusStrip1.Text = "statusBar";
    // 
    // toolStripStatusLabel1
    // 
    this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
    this.toolStripStatusLabel1.Size = new System.Drawing.Size(61, 17);
    this.toolStripStatusLabel1.Text = "Status Bar";
    // 
    // TreeDDC
    // 
    this.TreeDDC.Dock = System.Windows.Forms.DockStyle.Left;
    this.TreeDDC.Location = new System.Drawing.Point(0, 0);
    this.TreeDDC.Name = "TreeDDC";
    this.TreeDDC.Size = new System.Drawing.Size(97, 445);
    this.TreeDDC.TabIndex = 1;
    // 
    // BtnPointCtrl
    // 
    this.BtnPointCtrl.Location = new System.Drawing.Point(28, 93);
    this.BtnPointCtrl.Name = "BtnPointCtrl";
    this.BtnPointCtrl.Size = new System.Drawing.Size(202, 86);
    this.BtnPointCtrl.TabIndex = 2;
    this.BtnPointCtrl.Text = "???";
    this.BtnPointCtrl.UseVisualStyleBackColor = true;
    // 
    // BtnLogic
    // 
    this.BtnLogic.Location = new System.Drawing.Point(28, 282);
    this.BtnLogic.Name = "BtnLogic";
    this.BtnLogic.Size = new System.Drawing.Size(202, 86);
    this.BtnLogic.TabIndex = 4;
    this.BtnLogic.Text = "??";
    this.BtnLogic.UseVisualStyleBackColor = true;
    // 
    // BtnComm
    // 
    this.BtnComm.Location = new System.Drawing.Point(307, 93);
    this.BtnComm.Name = "BtnComm";
    this.BtnComm.Size = new System.Drawing.Size(202, 86);
    this.BtnComm.TabIndex = 3;
    this.BtnComm.Text = "??";
    this.BtnComm.UseVisualStyleBackColor = true;
    // 
    // BtnSystem
    // 
    this.BtnSystem.Location = new System.Drawing.Point(307, 282);
    this.BtnSystem.Name = "BtnSystem";
    this.BtnSystem.Size = new System.Drawing.Size(202, 86);
    this.BtnSystem.TabIndex = 5;
    this.BtnSystem.Text = "???";
    this.BtnSystem.UseVisualStyleBackColor = true;
    // 
    // panel1
    // 
    this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.panel1.Controls.Add(this.BtnSystem);
    this.panel1.Controls.Add(this.BtnComm);
    this.panel1.Controls.Add(this.BtnPointCtrl);
    this.panel1.Controls.Add(this.BtnLogic);
    this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.panel1.Location = new System.Drawing.Point(97, 0);
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(542, 445);
    this.panel1.TabIndex = 6;
    // 
    // MainForm
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(639, 467);
    this.Controls.Add(this.panel1);
    this.Controls.Add(this.TreeDDC);
    this.Controls.Add(this.statusStrip1);
    this.Name = "MainForm";
    this.Text = "MainForm";
    this.statusStrip1.ResumeLayout(false);
    this.statusStrip1.PerformLayout();
    this.panel1.ResumeLayout(false);
    this.ResumeLayout(false);
    this.PerformLayout();

}

但我相信唯一感兴趣的代码如下:

this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;


this.panel1.Controls.Add(this.BtnSystem);
this.panel1.Controls.Add(this.BtnComm);
this.panel1.Controls.Add(this.BtnPointCtrl);
this.panel1.Controls.Add(this.BtnLogic);

任何帮助将不胜感激。

4

3 回答 3

4

使用按钮的Anchor属性...

阅读它使用锚定和对接

面板在调整大小时本身不会重新定位控件,使用Anchor (Top, Bottom, Left, Right)当父面板调整大小时控件重新定位的属性......

或使用TableLayOutPanel

于 2012-06-15T04:59:08.313 回答
3

停靠面板只会调整面板的大小,不会重新定位面板内的控件。要让容器中的元素在调整容器大小后更改其位置,请查看Anchor属性。TableLayoutPanel顾名思义,还有一个表格格式的布局控件。

于 2012-06-15T04:57:17.253 回答
0

请将 FlowLayoutPanel 停靠在您的面板中,并将按钮放置在该 FlowLayoutPanel 中。设置布局方向属性。现在,当您调整主窗口的大小时,您的按钮也将调整大小。

于 2012-06-15T05:20:47.150 回答