2

我在表单上有一个标签和一个文本框。标签的内容是动态的,可能会将其边界溢出到其下方的文本框上。我想适当地动态增加表单的高度和文本框的顶部,以便标签内容将文本框“推”到表单上。通过将标签设置为 Autosize 并给它一个最大宽度,我想让它水平增长到表单 bu 的右边缘,然后垂直(向下)增长到它需要的程度。

我尝试这样做的代码是:

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”的返回值,因为它不是变量

那么我怎样才能做到这一点呢?

4

2 回答 2

4

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

于 2012-08-07T15:50:46.933 回答
0
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);
}
于 2012-08-07T16:09:48.490 回答