2

I set (100, 100) for size of a label in the properties and set (100, 100) for another label in my code.

But the sizes are diffrent.(code label is bigger than another)
also depends on my code the two label most sticked together. but result is diffrent!

this is my 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);
        }
    }
}

this is the result i get : (the left label have (100, 100) size and (0,0) location)

enter image description here

**Edit: this is my 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;
    }
}
Hamed
  • 313
  • 2
  • 15
  • It is overwritten somewhere...we have no idea where - we do not own all code. – Renatas M. Nov 23 '18 at 13:31
  • @Reniuz this is my complete code. this code is in Form_Load – Hamed Nov 23 '18 at 13:34
  • `(the left label have (100, 100) size and (0,0) location)` cannot be true if the right label has `cell.Size = new Size(100, 100); cell.Location = new Point(100, 0);`.If this was true, then the blocks would overlap (or at least 'touch' each other). This means, as Reniuz said, something else is going on in your code. Can you post your form designer generated code? – Jevgeni Geurtsen Nov 23 '18 at 13:34
  • @JevgeniGeurtsen that's my problem. I think green label have not real size – Hamed Nov 23 '18 at 13:36
  • 1
    `and set (100, 100) for another label in my code. ` where is that code? Your code sets color RED - there is no red label in picture. Please add your actual code so we could replicate the issue. – Renatas M. Nov 23 '18 at 13:37
  • 2
    @hamed we need more code. Post your designer code, in the solution explorer click the expander '+' next to your form entry and show us .Designer.cs – Jevgeni Geurtsen Nov 23 '18 at 13:38
  • @Reniuz sorry, that was a little mistake :) i chaned color – Hamed Nov 23 '18 at 13:39
  • Is this user control? Where is the code of green label? Could you add all relevant code please. – Renatas M. Nov 23 '18 at 13:42
  • @JevgeniGeurtsen done – Hamed Nov 23 '18 at 13:56
  • use `sizeF (100,100)` instead of `size(100,100)` for the label in the code. – preciousbetine Nov 23 '18 at 14:14

1 Answers1

2

Your location properties are invalid. In your designer:

// 
// Form1
// 
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); <--
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; <--
...
this.panel.ResumeLayout(false);
this.ResumeLayout(false);

The problem relies in the first two lines, the auto scaling will kick in as soon as your form is rendered, your label1 will be scaled because of the AutoScaleMode. This is not applied to your in code generated label, as the scaling happens after all the controls have been rendered on the form (and before you add the label programmatically). (source)

To fix the sizing issue; remove the AutoScale and AutoScaleDimensions from your designer code (or select the Form in the designer and set both properties back to their default values).

Community
  • 1
  • 1
Jevgeni Geurtsen
  • 3,133
  • 4
  • 17
  • 35