2

フォームにラベルとテキストボックスがあります。ラベルのコンテンツは動的であり、その境界がその下のテキストボックスにオーバーフローする可能性があります。フォームの高さとtextBoxの上部を必要に応じて動的に増やして、ラベルの内容がテキストボックスをフォームに「押し込む」ようにします。ラベルを[自動サイズ]に設定し、最大幅を指定することで、フォーム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.Size.Height の代わりに this.Height を使用し、textbox.Location.Y の代わりに textbox.Top を使用します。

于 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 に答える