将控件对齐到FlowLayout中心

Dav*_*vid 17 .net c# winforms

我的流程布局如下:

在此输入图像描述

我需要将窗体上的所有控件居中(换句话说,假设窗体的宽度为200. btnOpt1到btnOpt4应该Left从100减去按钮宽度的一半,而不是0.)

Nir*_*ngh 25

您可以通过两种方式完成,但每种方式都有一些限制.

  1. 使用Anchor财产
  2. 在帮助DockingAnchor属性的帮助下使用布局控件.

方法1:锚属性

默认情况下,控件锚定在窗体的左上角,这意味着当窗体大小发生更改时,它们与窗体左上角的距离将保持不变.如果将控件锚更改为左下角,则控件将在窗体调整时保持与窗体底部和左侧相同的距离.

在调整大小时,关闭方向上的锚点将使控件在该方向上居中.

示例:

public TestForm12()
{
   InitializeComponent();

   Button btn = new Button();
   btn.Width = this.Width - 10;
   btn.Height = 20;
   btn.Left = (this.ClientSize.Width - btn.Width) / 2;
   btn.Top = (this.ClientSize.Height - btn.Height) / 2;
   btn.Text = "click me";
   this.Controls.Add(btn);
   btn.Anchor = AnchorStyles.None;               

}
Run Code Online (Sandbox Code Playgroud)

2.使用布局控件

  1. 添加TableLayout控件,将其Dock属性设置为Fill.
  2. 添加1行,大小类型样式百分比100%
  3. 添加3列Column1(大小类型 - 百分比(100%)),Column2(大小类型 - 绝对(200px)),Column3(大小类型 - 百分比(100%)).
  4. 现在将Panel Control添加到Column2并将其Dock属性设置为Fill
  5. 向此控件添加按钮并根据需要设置其大小,并将其Anchor属性设置为AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top

示例 - 表单的Designer.cs代码段.

private void InitializeComponent()
 {
     this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
     this.panel1 = new System.Windows.Forms.Panel();
     this.button1 = new System.Windows.Forms.Button();
     this.button2 = new System.Windows.Forms.Button();
     this.tableLayoutPanel1.SuspendLayout();
     this.panel1.SuspendLayout();
     this.SuspendLayout();
     // 
     // tableLayoutPanel1
     // 
     this.tableLayoutPanel1.ColumnCount = 3;
     this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
     this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 200F));
     this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
      this.tableLayoutPanel1.Controls.Add(this.panel1, 1, 0);
      this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
      this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
      this.tableLayoutPanel1.Name = "tableLayoutPanel1";
      this.tableLayoutPanel1.RowCount = 1;
      this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
      this.tableLayoutPanel1.Size = new System.Drawing.Size(284, 262);
      this.tableLayoutPanel1.TabIndex = 0;
      // 
      // panel1
      // 
      this.panel1.Controls.Add(this.button2);
      this.panel1.Controls.Add(this.button1);
      this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
      this.panel1.Location = new System.Drawing.Point(45, 3);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(194, 256);
      this.panel1.TabIndex = 0;
      // 
      // button1
      // 
      this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.button1.Location = new System.Drawing.Point(3, 9);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(188, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.UseVisualStyleBackColor = true;
    // 
    // button2
    // 
    this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.button2.Location = new System.Drawing.Point(3, 38);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(188, 23);
    this.button2.TabIndex = 0;
    this.button2.Text = "button1";
    this.button2.UseVisualStyleBackColor = true;
    // 
    // TestForm11
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 262);
    this.Controls.Add(this.tableLayoutPanel1);
    this.Name = "TestForm11";
    this.Text = "TestForm11";
    this.tableLayoutPanel1.ResumeLayout(false);
    this.panel1.ResumeLayout(false);
    this.ResumeLayout(false);

}

 #endregion

private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
Run Code Online (Sandbox Code Playgroud)

希望这有帮助..


Ser*_*kiy 9

我会改用TableLayoutPanel:

  • 将TableLayoutPanel放在表单上
  • 将停靠样式设置Fill为面板
  • 在面板内只留一列
  • 为每个按钮创建行(并将按钮放到表格单元格)
  • 设置行大小类型 Autosize
  • 将停靠样式设置Fill为每个按钮,除了最后一个按钮
  • 将停靠方式设置Top为最后一个按钮

在您的解决方案中,您应该迭代flowLayoutPanel控件而不是表单控件.还可以考虑从宽度中减去水平边距和填充:

foreach (Control control in flowLayoutPanel.Controls)
{
    control.Size = new Size(flowLayoutPanel.Width - control.Margin.Horizontal,
                            control.Height); 
}
Run Code Online (Sandbox Code Playgroud)

但我建议你改用TableLayoutPanel.