Moose
Moose

Reputation: 755

How to centre align the content of windows form application?

I am developing a simple windows form application. But I am not able to center align the contents of the form. here are the images: enter image description here

Here's the code of the form:

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;
}

}

I tried using this,

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

But even that did not help.

Upvotes: 1

Views: 8266

Answers (3)

Prasanna Gulhane
Prasanna Gulhane

Reputation: 80

you take a DIV than assign align inline property for label

Upvotes: -1

inquisitive
inquisitive

Reputation: 3639

One way to do is to use TableLayoutPanel as @maam27 commented. Another way is to use the Anchor property of the control. By default a control is anchored to the top-left corner of the parent form. So when form resizes, that point remains fixed. You may choose to break the left anchor. Manually center-align your components in designer. After that if your form resizes, then the relative position will remain unchanged.

Upvotes: 1

maam27
maam27

Reputation: 434

I suggest you try the TableLayoutPanel, if you add one you shall get a small 2x2 grid. If you right click and say edit rows and columns you can add a third column and the amount of rows you need. for the settings of the panel I'd suggest something along these lines:

column1 : 50%

column2 : autosize

column3 : 50%

This will make it so the middle column is the minimal required size for your controls, and the remainder of the width is divided over the other 2 columns. Don't forget to dock the tableLayoutPanel to the form.

Thought I'd post it as an awnser instead of the comment I left before.

Upvotes: 2

Related Questions