C# 如何动态地垂直增长窗体,并将文本框向下移动适当的数量?

C# 如何动态地垂直增长窗体,并将文本框向下移动适当的数量?,c#,winforms,dynamic,textbox,label,C#,Winforms,Dynamic,Textbox,Label,我在表单上有一个标签和一个文本框。标签的内容是动态的,可能会将其边界溢出到其下方的文本框中。我想根据需要动态增加表单的高度和文本框的顶部,以便标签内容将文本框向下推到表单上。通过将标签设置为Autosize并为其提供最大宽度,我希望允许它仅水平地增长到表单的右边缘,然后垂直地向下增长 我尝试此操作的代码是: int bottomOfLabel = label1.Location.X + label1.Size.Height; int topOfTextBox = textBox1.Locatio

我在表单上有一个标签和一个文本框。标签的内容是动态的,可能会将其边界溢出到其下方的文本框中。我想根据需要动态增加表单的高度和文本框的顶部,以便标签内容将文本框向下推到表单上。通过将标签设置为Autosize并为其提供最大宽度,我希望允许它仅水平地增长到表单的右边缘,然后垂直地向下增长

我尝试此操作的代码是:

int bottomOfLabel = label1.Location.X + label1.Size.Height;
int topOfTextBox = textBox1.Location.Y;
int currentHeightOfForm = this.Size.Height;
int currentTopOfTextBox = texBox1.Location.Y;

if (bottomOfLabel >= topOfTextBox)
{
    int heightToAdd = bottomOfLabel - topOfTextBox;
    this.Size.Height = currentHeightOfForm + heightToAdd;
    textbox.Location.Y = currentTopOfTextBox + heightToAdd;
}
…但我发现了这些错误:

无法修改“System.Windows.Forms.Form.Size”的返回值,因为它不是变量

-以及:

无法修改“System.Windows.Forms.Control.Location”的返回值,因为它不是变量


那么我如何才能做到这一点呢?

用this.Height代替this.Size.Height,用textbox.Top代替textbox.Location.Y。

用this.Height代替this.Size.Height,用textbox.Top代替textbox.Location.Y

const int WIGGLE_ROOM = 4;
int bottomOfLabel = label1.Location.Y + label1.Size.Height;
int currentHeightOfForm = this.Size.Height;
int widthOfForm = this.Size.Width;
int leftSideOfTextBox = textBox1.Location.X;
int currentTopOfTextBox = textBox1.Location.Y;

if (bottomOfLabel >= (currentTopOfTextBox - WIGGLE_ROOM)) {
    int heightToAdd = (bottomOfLabel - currentTopOfTextBox) + WIGGLE_ROOM;
    this.Size = new Size(widthOfForm, currentHeightOfForm + HeightToAdd);
     textBox1.Location = new Point(leftSideOfTextBox, currentTopOfTextBox +   
          heightToAdd);
}