【问题标题】:C# Buttons not repositioning themselvesC#按钮不重新定位自己
【发布时间】:2012-06-18 04:12:20
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

  • 将至少一个答案标记为已接受...如果问题已解决

标签: c# .net button panel


【解决方案1】:

使用按钮的Anchor 属性...

阅读Working with Anchoring and Docking

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

或使用TableLayOutPanel

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-09
        • 1970-01-01
        • 2018-06-19
        • 1970-01-01
        • 2015-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多