如何居中对齐windows表单申请的内容?

How to centre align the content of windows form application?

我正在开发一个简单的 windows 表单应用程序。但我无法居中对齐表格的内容。这是图片:

表单代码如下:

namespace WindowsFormsApplication1
{
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.button1 = new System.Windows.Forms.Button();
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(1, 1);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 20);
        this.textBox1.TabIndex = 0;
        // 
        // button1
        // 
        this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));
        this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
        this.button1.FlatAppearance.BorderColor = System.Drawing.SystemColors.Highlight;
        this.button1.FlatAppearance.BorderSize = 5;
        this.button1.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.HotTrack;
        this.button1.ForeColor = System.Drawing.Color.Coral;
        this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));
        this.button1.Location = new System.Drawing.Point(330, 275);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(97, 123);
        this.button1.TabIndex = 1;
        this.button1.UseMnemonic = false;
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // pictureBox1
        // 
        this.pictureBox1.Enabled = false;
        this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
        this.pictureBox1.ImageLocation = "";
        this.pictureBox1.Location = new System.Drawing.Point(270, 27);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(221, 176);
        this.pictureBox1.TabIndex = 2;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Font = new System.Drawing.Font("OCR A Extended", 14.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label1.Location = new System.Drawing.Point(251, 226);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(251, 20);
        this.label1.TabIndex = 3;
        this.label1.Text = "Please Touch Your Card";
        this.label1.Click += new System.EventHandler(this.label1_Click);
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.ForeColor = System.Drawing.Color.Red;
        this.label2.Location = new System.Drawing.Point(359, 433);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(35, 13);
        this.label2.TabIndex = 4;
        this.label2.Text = "label2";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.Highlight;
        this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        this.ClientSize = new System.Drawing.Size(752, 502);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.pictureBox1);
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds;
        this.Text = "Form1";
        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;

    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.PictureBox pictureBox1;
}

}

我试过用这个,

  this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

但即便如此也无济于事。

我建议您尝试 TableLayoutPanel,如果您添加一个,您将得到一个 2x2 的小网格。如果您右键单击并说编辑行和列,您可以添加第三列和您需要的行数。对于面板的设置,我建议遵循以下原则:

column1 : 50%

column2 : autosize

column3 : 50%

这将使中间列成为您的控件所需的最小尺寸,其余宽度将分配给其他 2 列。不要忘记将 tableLayoutPanel 停靠到表单。

以为我会 post 它作为遮阳篷而不是我之前留下的评论。

一种方法是使用 TableLayoutPanel 作为@maam27 评论。另一种方法是使用 Anchor 属性 控件。默认情况下,控件锚定在父窗体的左上角。因此,当表单调整大小时,该点保持不变。您可以选择打破左锚。在设计器中手动居中对齐您的组件。之后,如果您的表单调整大小,则相对位置将保持不变。

你采用 DIV 而不是为标签

分配内联对齐 属性