简体   繁体   中英

Positions and sizes of controls in Windows Forms differ between Visual Studio Designer and programmatic instantiation

when I create a Windows Forms Application using Visual Studio (Community 2019) and the Designer tool it appears that controls have not the correct sizes and positions. Eg positioning a TextBox at (100, 80) with size (100, 22) is actually not positioned at the desired location. Also the x-dimension of the size is actually 75 and not 100. If I instantiate a similar TextBox programmatically with the same values (just shifted in y-position) it is placed more to the right and it is bigger in size compared to the other one, but this time with the correct values.

See the attached pictures.

通过 Designer 设置第一个 TextBox 的属性 实际输出

I have created a small test application in order to demonstrate this.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsTest
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.Designer.cs

namespace WindowsFormsTest
{
    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.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(100, 80);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 22);
            this.textBox1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(46, 17);
            this.label1.TabIndex = 1;
            this.label1.Text = "label1";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsTest
{
    public partial class Form1 : Form
    {
        private TextBox textBox2;

        public Form1()
        {
            InitializeComponent();
            TestBox();
            this.MouseMove += new MouseEventHandler(MouseMoved);
        }


        private void TestBox()
        {
            textBox2 = new TextBox() {
                Location = new System.Drawing.Point(100, 104),
                Name = "textBox2",
                Size = new System.Drawing.Size(100, 22),
                TabIndex = 1
            };

            this.Controls.Add(textBox2);
        }

        private void MouseMoved(object sender, MouseEventArgs evt)
        {
            label1.Text = "X = " + evt.X.ToString() + ", Y = " + evt.Y.ToString();
        }
    }
}

What am I missing here? How can I achieve that the control via designer has the desired size and position (like the second TextBox) or vice versa?

Thanks in advance for any help!

I had this exact problem a few months back. It's your monitor (or technically you're display resolution and scaling). You've got a high DPI monitor I'm guessing. You can see in VS there is a little notification about scaling - the VS windows forms designer doesn't seem to handle scaling well.

I fixed this by firstly discarding any changes I had made to my form first and closing VS. Then changed my monitor resolution back to standard HD (because without scaling my 4k monitor made things too small - you could just try standard 100% scaling) and then opened my VS and made my changes and all worked fine.

This answer here https://stackoverflow.com/a/12406133/6915929 seems to be describing a way of setting things up on the form to handle DPI changes, but I haven't tried that myself

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM