简体   繁体   English

组件的大小根据屏幕分辨率c#调整大小

[英]The size of the components resize with the screen resolution c#

I have 5 textboxes in winform and when the resolution is 1366x768, their position and size are correct. 我在winform中有5个文本框,分辨率为1366x768时,它们的位置和大小正确。 But, once I have changed the resolution to 1024x768, two of the textboxes are overlap with each other. 但是,一旦我将分辨率更改为1024x768,两个文本框就会相互重叠。

My question is: How could I make the width of the textboxes scale depending on the screen resolution? 我的问题是:如何根据屏幕分辨率缩放文本框的宽度?

Here is the images: 这是图像:

1366x768 screen resolution: 1366x768屏幕分辨率:

在此处输入图片说明

1024x768 screen resolution: 1024x768屏幕分辨率:

在此处输入图片说明

Here for the 1024x768 screen resolution, the description textbox overlap with the quantity textbox. 对于1024x768屏幕分辨率,此处的描述文本框与数量文本框重叠。

Here is the code that I am using: 这是我正在使用的代码:

void SetComponents()
        {
            _screen = Screen.PrimaryScreen.WorkingArea;

            this.label1.Text = "Product Code";

            this.label1.Location = new Point(40, (_screen.Height - _screen.Height) + 125);

            this.textBox1.Location = new Point(25, (_screen.Height - _screen.Height) + 150); // textbox for the product code

            this.label2.Text = "Quantity";

            this.label2.Location = new Point(215, (_screen.Height - _screen.Height) + 125);

            this.numericUpDown1.Location = new Point(185, (_screen.Height - _screen.Height) + 150); // numeric up down for the quantity

            this.label3.Text = "Description";

            this.textBox2.Size = new Size((_screen.Width / 2) + 100, 20); // textbox for the description

            this.label3.Location = new Point((_screen.Width / 2) + 50, (_screen.Height - _screen.Height) + 125);

            this.textBox2.Location = new Point((_screen.Width / 2) - 325, (_screen.Height - _screen.Height) + 150); // textbox for the description

            this.label4.Text = "Price (@ Rp)";

            this.label4.Location = new Point((_screen.Width - 150), (_screen.Height - _screen.Height) + 125);

            this.textBox3.Location = new Point((_screen.Width - 165), (_screen.Height - _screen.Height) + 150); // textbox for the price

            this.label5.Text = "Date / Time: ";

            this.textBox4.Size = new Size(145, 20); // textbox for the date / time

            this.label5.Location = new Point((_screen.Width - 275), (_screen.Height - _screen.Height) + 58);

            this.textBox4.Location = new Point((_screen.Width - 200), (_screen.Height - _screen.Height) + 55); // textbox for the date / time

            this.label6.Text = _welcomeText + UserInformation.CurrentLoggedInUser + " - " + UserInformation.CurrentLoggedInUserType;

            this.label6.Location = new Point((_screen.Width - _screen.Width) + 10, (_screen.Height - _screen.Height) + 30);

            this.button1.Text = "Submit";

            this.button1.Location = new Point((_screen.Width / 2) - 150, (_screen.Height - _screen.Height) + 185);

            this.button2.Text = "Reset";

            this.button2.Location = new Point((_screen.Width / 2) + 100, (_screen.Height - _screen.Height) + 185);

            this.button3.Text = "Delete";

            this.button3.Location = new Point((_screen.Width / 2) + 20, (_screen.Height - _screen.Height) + 185);

            this.button4.Text = "Edit";

            this.button4.Location = new Point((_screen.Width / 2) - 70, (_screen.Height - _screen.Height) + 185);

            this.button5.Text = "Update";

            this.button5.Location = new Point((_screen.Width / 2) - 70, (_screen.Height - _screen.Height) + 185);

            this.button6.Text = "Cancel";

            this.button6.Location = new Point((_screen.Width / 2) + 100, (_screen.Height - _screen.Height) + 185);

            this.button7.Text = "Information";

            this.button7.Location = new Point(10, (_screen.Height - _screen.Height) + 185);

            this.dataGridView1.Size = new Size((_screen.Width) - 40, (_screen.Height - _screen.Height) + 460);

            this.dataGridView1.Location = new Point((_screen.Width - _screen.Width) + 10, (_screen.Height - _screen.Height) + 225);
        }

I really appreciate your answer 非常感谢您的回答

Thank you 谢谢

The usual approach to this is to use the container controls from winform TableControlLayout, then bind those control to your form will the Fill property. 通常的方法是使用winform TableControlLayout中的容器控件,然后将这些控件绑定到您的窗体上,将Fill属性。 Inside the Container, add your usual controls which should snap to the Container and resize as you want. 在容器内,添加通常的控件,这些控件应对齐到容器并根据需要调整大小。

In your case, you seem to be using a DataGridView which fills the whole bottom of the form, which should be correct. 在您的情况下,您似乎正在使用填充表格整个底部的DataGridView,这应该是正确的。

Inside the DataGridView column editor, you can edit the fill property of each column either in percentage, absolute value or tell them to fill remaining space 在DataGridView列编辑器内,您可以按百分比,绝对值或告诉它们填充剩余空间的方式编辑每列的fill属性。

EDIT: 编辑:

Seems like I misread your question. 好像我误解了你的问题。 Anyway it is all about the Containers and the Fill property 无论如何,这都是关于Containers和Fill属性的

Try this: 尝试这个:

ctl.Height = ctl.Height * (My.Computer.Screen.Bounds.Height / 768);
ctl.Width = ctl.Width * (My.Computer.Screen.Bounds.Width / 1366);

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

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