C# 如何将windows窗体应用程序的内容居中对齐?
我正在开发一个简单的windows窗体应用程序。但我无法将表单的内容居中对齐。以下是图片: 以下是表格的代码:C# 如何将windows窗体应用程序的内容居中对齐?,c#,windows-forms-designer,C#,Windows Forms Designer,我正在开发一个简单的windows窗体应用程序。但我无法将表单的内容居中对齐。以下是图片: 以下是表格的代码: namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer compo
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;
}
}
但即使这样也无济于事 我建议您尝试使用
表格布局面板
,如果添加一个,您将得到一个小的2x2网格。如果右键单击并说“编辑行和列”,则可以添加第三列以及所需的行数。对于面板的设置,我建议如下:
第1栏:50%
第2列:自动调整大小
第3栏:50%
这将使中间的列成为控件所需的最小大小,剩余的宽度将除以其他2列。别忘了将tableLayoutPanel固定到表单上
我想我应该把它作为awnser发布,而不是我之前留下的评论。一种方法是使用@maam27评论的
TableLayoutPanel
。另一种方法是使用控件的Anchor
属性。默认情况下,控件定位到父窗体的左上角。所以当窗体调整大小时,该点保持不变。您可以选择断开左锚。在designer中手动将零部件居中对齐。之后,如果窗体调整大小,则相对位置将保持不变。您使用DIV,而不是为label指定align inline属性。是否尝试获取窗体中心的所有控件?在文本框和那些控件中,不只是文本是,除了文本框。但是右边的属性框没有这个选项。当我启动表单时,它会以全屏模式启动,在全屏模式下,我希望看到除表单中心的文本框之外的所有内容。您是否尝试过表格布局面板
?您可以将其放置在整个窗体上,方法是将其停靠,并将每个控件放在其自己的单元格中。然后可以设置列大小和行大小。如果创建3列(第1列:50%,第2列:自动调整大小,第3列:50%),并将所有需要居中的控件放在第二列,文本框放在第一列,使其保持在左侧不,我没有。谢谢。这是winforms的问题,不是web。这里没有div:(我觉得如果你只想在宽度上居中,这样做很好,但如果OP也想在高度上居中,这可能已经不够了。同样,将它放在精确的中心可能会有点麻烦。我仍然认为在这样的东西上有多个选项是好的。我使用了这个。这非常完美。@RiteshThakur g小伙子,我能帮忙。
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;