C# : size in properties is diffrent from code

标签 c# windows-forms-designer

我在属性中为标签的大小设置了 (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)

enter image description here

**编辑:这是我的 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)

解决尺寸问题;从设计器代码中删除 AutoScaleAutoScaleDimensions(或在设计器中选择 Form 并将这两个属性设置回默认值)。

关于C# : size in properties is diffrent from code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53447601/

相关文章:

c# - Visual Studio Windows 窗体 "Value does not fall within the expected range."

c# - 用多个字符串数组拆分字符串

c# - 无法从 'object' 转换为 'decimal'

c# - Entity Framework EntityState 和 SaveChanges 行为无需更改

c# - 以 Windows 形式播放 youtube 视频

C# Windows Forms ListView 获取选中的行值

c# - 使用 Visual Studio Designer 在 TableLayoutPanel 中居中 Windows 窗体控件

c# - VS 2010 设计器中控件对齐的逆序

c# - 如何将表情符号从 bot 发送到 Microsoft 团队中的用户

c# - 发送两个文本框而不是动态发送一个