2015-04-02 79 views
2

我正在開發一個簡單的Windows窗體應用程序。但是我無法居中對齊表單的內容。這裏是圖像:enter image description here如何居中對齊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; 

但即使這樣,也沒有幫助。

+0

你試圖讓你的窗體中心的所有控件?如在文本框和這些控件中,不僅僅是文本 – maam27 2015-04-02 07:30:09

+0

是的,除了文本框。但是右側的屬性框沒有這個選項。當我啓動表單時,它會以全屏模式啓動,在全屏模式下,我希望除了窗體中心的文本框外,您可以看到的所有內容。 – 2015-04-02 07:31:05

+0

你嘗試過'TableLayoutPanel'嗎?你可以通過對接將它放在整個表單上,並將每個控件放在它自己的單元格中。那麼您可以設置列大小和行大小。如果創建3列(第1列:50%,第2列:自動調整大小,第3列:50%),並將需要居中的所有控件放在第二列中,並將第一列中的文本框放在左側 – maam27 2015-04-02 07:33:10

回答

2

我建議你試試TableLayoutPanel,如果你添加一個,你會得到一個小的2x2網格。如果您右鍵單擊並說出編輯行和列,則可以添加第三列和所需的行數。對於面板的設置,我建議沿着這些路線的東西:

列1:50%

列2:自動調整

欄3:50%

這將使它所以中間一列是您的控件所需的最小尺寸,其餘的寬度則分成另外兩列。不要忘記將tableLayoutPanel停靠在表單上。

以爲我會把它發佈爲awnser而不是我之前留下的評論。

+0

我用過這個。這是完美的。 – 2015-04-02 07:57:48

+0

@RiteshThakur很高興我能夠提供幫助。 – maam27 2015-04-02 08:02:37

1

一種方法是使用TableLayoutPanel作爲@ maam27評論。另一種方法是使用控件的Anchor屬性。默認情況下,控件將錨定到父窗體的左上角。所以當表格調整大小時,這一點仍然是固定的。你可以選擇打破左側的錨點。手動在設計器中心對齊您的組件。之後如果你的表格調整大小,那麼相對位置將保持不變。

+0

我覺得這樣做可以很好地工作,如果你想把事情放在寬度上,但是如果OP想把它放在高度上,它可能不夠了。把它放在正確的中心也可能會有點麻煩。我仍然認爲在這樣的事情上有多種選擇是很好的。 – maam27 2015-04-02 07:45:28

+0

當然。 * ...一種方式和另一種方式... * – inquisitive 2015-04-02 07:46:40

+0

這幫了我。 – 2015-04-02 07:57:26

0

你拿一個DIV比分配對齊內聯屬性的標籤

+0

這是一個winforms問題,而不是web。這裏我們沒有divs :( – inquisitive 2015-04-02 07:42:15

相關問題