vpv
vpv

Reputation: 938

How to increase the form height along with textbox height in c# winform app?

I have a multiline TextBox in my WinForm app having Multiline=True, Scrollbar=Vertical & WordWrap=True. What I want to do is, whenever I reach the bottom of the textbox (either by entering text or by pressing enter), I want the form height to increased along with the textbox height (exactly same to the StickyNote app in Windows). I don't know where to start? Please help.

Edit

Below is my code

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

Upvotes: 0

Views: 64

Answers (0)

Related Questions