简体   繁体   English

Windows Forms 中控件的位置和大小在 Visual Studio Designer 和编程实例化之间有所不同

[英]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.当我使用 Visual Studio(Community 2019)和 Designer 工具创建 Windows Forms 应用程序时,似乎控件的大小和位置不正确。 Eg positioning a TextBox at (100, 80) with size (100, 22) is actually not positioned at the desired location.例如,将大小为 (100, 22) 的 TextBox 定位在 (100, 80) 实际上并未定位在所需的位置。 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.此外,大小的 x 维度实际上是 75 而不是 100。如果我用相同的值(只是在 y 位置移动)以编程方式实例化一个类似的 TextBox,它会更靠右放置,并且与另一个,但这次使用正确的值。

See the attached pictures.见附图。

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

I have created a small test application in order to demonstrate this.为了演示这一点,我创建了一个小型测试应用程序。

Program.cs程序.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 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 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?如何通过设计器实现控件具有所需的大小和 position(如第二个文本框),反之亦然?

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.我猜你有一个高 DPI 显示器。 You can see in VS there is a little notification about scaling - the VS windows forms designer doesn't seem to handle scaling well.您可以在 VS 中看到一些关于缩放的通知 - VS windows forms 设计器似乎不能很好地处理缩放。

I fixed this by firstly discarding any changes I had made to my form first and closing VS.我通过首先丢弃我对表单所做的任何更改并关闭 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.然后将我的显示器分辨率改回标准高清(因为不缩放我的 4k 显示器会使事情变得太小 - 你可以尝试标准 100% 缩放)然后打开我的 VS 并进行更改,一切正常。

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这个答案https://stackoverflow.com/a/12406133/6915929似乎描述了一种在表单上设置东西以处理 DPI 更改的方法,但我自己没有尝试过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Visual Studio Designer将Table窗体控件置于TableLayoutPanel中 - Centering Windows Forms Controls inside TableLayoutPanel with Visual Studio Designer 从扩展程序在Visual Studio Designer中的windows.forms.controls上绘制装饰 - Draw adornments on windows.forms.controls in Visual Studio Designer from an extension 在绘制控件后,如何将光标放回Visual Studio 2010 RC Windows窗体设计器中? - How do I get the cursor back in Visual Studio 2010 RC Windows Forms designer after drawing controls? Visual Studio Windows Forms 设计器错误 - Visual Studio Windows Forms Designer Error Visual Studio 中未显示设计器视图 windows forms - Designer view not showing in Visual Studio windows forms Visual Studio形成设计师 - Visual Studio forms designer 如何重新同步Visual Studio 2008 Windows窗体设计器? - How do I resync the Visual Studio 2008 Windows Forms Designer? Windows窗体设计器重命名复制/粘贴控件 - Windows forms designer renaming copy/pasted controls 创建使您可以从Visual Studio Forms Designer中选择当前表单中其他控件的下拉菜单 - Create dropdowns that let you select other controls in the current form from the Visual Studio Forms Designer Visual Studio 2017设计器视图未显示控件 - Visual Studio 2017 Designer View not Showing Controls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM