我在属性中为标签的大小设置了 (100, 100),并在代码中为另一个标签设置了 (100, 100)。
但是尺寸不同。(代码标签比另一个大)
还取决于我的代码,两个标签最粘在一起。但结果不同!
这是我的 Form1.cs:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace BBSquare
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Label cell = new Label();
cell.Size = new Size(100, 100);
cell.Location = new Point(100, 0);
cell.BackColor = Color.Blue;
panel.Controls.Add(cell);
}
}
}
这是我得到的结果: (左侧标签的大小为 (100, 100),位置为 (0,0)
**编辑:这是我的 Form1.Designer.cs **
namespace BBSquare
{
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()
{
this.panel = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.panel.SuspendLayout();
this.SuspendLayout();
//
// panel
//
this.panel.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.panel.Controls.Add(this.label1);
this.panel.Location = new System.Drawing.Point(100, 141);
this.panel.Name = "panel";
this.panel.Size = new System.Drawing.Size(600, 600);
this.panel.TabIndex = 0;
//
// label1
//
this.label1.BackColor = System.Drawing.Color.Green;
this.label1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 100);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Red;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.ClientSize = new System.Drawing.Size(782, 753);
this.Controls.Add(this.panel);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Label label1;
}
}
最佳答案
您的位置属性无效。在你的设计器中:
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); <--
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; <--
...
this.panel.ResumeLayout(false);
this.ResumeLayout(false);
问题在于前两行,一旦呈现表单,自动缩放就会启动,您的 label1
将由于 AutoScaleMode
而缩放。这不适用于您在代码中生成的标签,因为缩放是在表单上呈现所有控件之后(以及以编程方式添加标签之前)发生的。
(source)
解决尺寸问题;从设计器代码中删除 AutoScale
和 AutoScaleDimensions
(或在设计器中选择 Form
并将这两个属性设置回默认值)。
关于C# : size in properties is diffrent from code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53447601/