0

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;
    }
vpv
  • 920
  • 2
  • 20
  • 46
  • You can use `Anchor` Property. – Shell Mar 06 '14 at 07:15
  • @HansPassant, I applied your code in my solution, but this doesn't change the height of either the textbox or the form. The text is still scrolling upwards. – vpv Mar 06 '14 at 07:22
  • You'll have the modify the if() statement at the bottom, the one that prevents it from getting bigger than the form. – Hans Passant Mar 06 '14 at 10:40

0 Answers0