0

WinForm アプリにMultiline=TrueScrollbar=Vertical 、 WordWrap =Trueを持つ複数行の TextBoxがあります。私がしたいのは、(テキストを入力するか、Enter キーを押して) テキスト ボックスの下部に到達するたびに、フォームの高さをテキスト ボックスの高さと一緒に増やしたいことです ( Windows の StickyNote アプリとまったく同じです)。どこから始めたらいいのかわからない?助けてください。

編集

以下は私のコードです

private void txtNote_TextChanged(object sender, EventArgs e)
{
    int currentHeight = txtNote.Height;
    int newHeight = 0;
    newHeight = txtNote.PreferredHeight * txtNote.Lines.Length;
    if (newHeight > currentHeight)
    {   
        //** Sriram's suggestion
        //this.ClientSize = new Size(this.ClientSize.Width, txtNote.PreferredHeight * txtNote.Lines.Length);

        //** Hans's suggestion
        Size sz = new Size(txtNote.ClientSize.Width, int.MaxValue);
        TextFormatFlags flags = TextFormatFlags.WordBreak;
        int padding = 3;
        int borders = txtNote.Height - txtNote.ClientSize.Height;
        sz = TextRenderer.MeasureText(txtNote.Text, txtNote.Font, sz, flags);
        int h = sz.Height + borders + padding;
        if (txtNote.Top + h > this.ClientSize.Height - 10)
        {
            h = this.ClientSize.Height - 10 - txtNote.Top;
        }
        txtNote.Height = h;
    }
4

0 に答える 0