【问题标题】:The size of the components resize with the screen resolution c#组件的大小随屏幕分辨率调整大小 c#
【发布时间】:2014-10-31 08:52:48
【问题描述】:

我在 winform 中有 5 个文本框,当分辨率为 1366x768 时,它们的位置和大小是正确的。但是,一旦我将分辨率更改为 1024x768,两个文本框就会相互重叠。

我的问题是:如何根据屏幕分辨率调整文本框的宽度?

图片如下:

1366x768 屏幕分辨率:

1024x768 屏幕分辨率:

这里对于 1024x768 的屏幕分辨率,描述文本框与数量文本框重叠。

这是我正在使用的代码:

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);
        }

非常感谢您的回答

谢谢

【问题讨论】:

  • 在设计时使用尽可能低的分辨率(在您的示例中为 1024x768)以确保控件不会重叠。
  • 如果您不执行任何操作,控件将自动调整大小而不会重叠。还是您希望每个分辨率的尺寸保持不变?
  • 分辨率在 1366 x 768 以上时尺寸保持不变,但如果分辨率低于 1366 x 768,则尺寸将自动调整大小以免相互重叠,先生@γηράσκωδ'αείπολλάδιδασκόμε
  • 分辨率与重叠无关。 100 像素 文本框将始终100 像素。改变的是pixel 的大小。当分辨率降低时,它会得到bigger。最终图像完全相同(没有重叠)但更大!你的SetComponents() 函数导致了问题,这就是我说不要做任何事情的原因。我的意思是不要使用这个功能。

标签: c# winforms


【解决方案1】:

通常的方法是使用来自 winform TableControlLayout 的容器控件,然后将这些控件绑定到您的表单将填充属性。在 Container 内,添加您常用的控件,这些控件应与 Container 对齐并根据需要调整大小。

在您的情况下,您似乎正在使用填充整个表单底部的 DataGridView,这应该是正确的。

在 DataGridView 列编辑器中,您可以按百分比、绝对值或告诉它们填充剩余空间来编辑每列的填充属性

编辑:

好像我误读了你的问题。无论如何,这都是关于 Containers 和 Fill 属性的

【讨论】:

  • 问题是顶部的文本框,黑色标题所在的位置,而不是数据网格。
【解决方案2】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2011-02-17
    • 2011-04-10
    • 2014-09-21
    • 1970-01-01
    • 2011-12-27
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多