如何居中对齐windows窗体应用程序的内容?

时间:2015-04-02 07:26:26

标签: c# windows-forms-designer

我正在开发一个简单的Windows窗体应用程序。但我无法对齐表单的内容。这是图像:enter image description here

以下是表格的代码:

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;

但即便如此也无济于事。

3 个答案:

答案 0 :(得分:2)

我建议您尝试TableLayoutPanel,如果添加一个,您将获得一个小的2x2网格。如果右键单击并说出编辑行和列,则可以添加第三列以及所需的行数。对于小组的设置,我建议这些内容:

  

第1栏:50%

     

column2:autosize

     

第3栏:50%

这将使得中间列是控件所需的最小尺寸,并且宽度的其余部分除以其他2列。别忘了将tableLayoutPanel停靠在表单上。

我以为将其发布为芒果而不是我之前留下的评论。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

你选择DIV而不是为标签指定对齐内联属性