C#按钮没有重新定位自己

时间:2012-06-15 04:52:31

标签: c# .net button panel

我在面板中包含了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);

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:4)

使用按钮的Anchor属性...

阅读Working with Anchoring and Docking

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

或使用TableLayOutPanel

答案 1 :(得分:3)

停靠面板只会调整面板的大小,而不是重新定位面板中的控件。要在调整容器大小后让容器中的元素更改其位置,请查看Anchor属性。还有TableLayoutPanel,正如其名称所示,它可以采用表格格式进行布局控制。

答案 2 :(得分:0)

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