Form does not auto grow if textbox is docked to fill it

Posted on 2012-09-06
Last Modified: 2012-09-06
I have a windows form with a multiline textbox control on it, the same size as the form. What I want is for textbox to expand downwards as the user types In more text to fill it. To do that I have the following code in TextChanged event:

 private void textBox1_TextChanged(object sender, EventArgs e)
            const int padding = 3;
            //get number of lines (first line is 0)    
            int numLines = this.textBox1.GetLineFromCharIndex(this.textBox1.TextLength) + 1;
            //get border thickness     
            int border = this.textBox1.Height - this.textBox1.ClientSize.Height;
            //set height (height of one line * number of lines + spacing)     
            this.textBox1.Height = this.textBox1.Font.Height * numLines + padding + border;

Open in new window

I aslo set Form's property AutoSize to true but the form only grows with the textbox automatically if textbox is not docked to fill the form. And I do need to dock my textbox.

Why isn't my form growing when the textbox is docked and what can I do to fix that?
Question by:YZlat
    LVL 85

    Accepted Solution

    When you Dock the TextBox (to fill I assume), it tells that TextBox to "maximize" in the parent container.  Since the TextBox is reacting to the Form's size, this means the TextBox will NEVER be bigger than the Form.  ...therefore the Form will never grow.

    Additionally, with Dock set to Fill, the TextBox will ignore any attempts to manually change its size.

    So you've got some mutually exclusive sets of rules there.   =\

    What are you really after?...are all of this rules correct?
    (1) TextBox enlarges to accommodate all typed Text.
    (2) Form enlarges to accommodate bigger TextBox.
    (3) Form and TextBox enlarge, when the Form is resized by the user, possibly resulting in a TextBox that is larger than all of the typed Text.
    (4) Form and TextBox shrink, when the Form is resized by user, but never small enough to obscure any of the typed Text.
    LVL 35

    Author Comment

    (1) and (2) are correct,

    form cannot be resized by the user and neither textbox nor form can shrink
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Okey dokey...

    Try this approach then.
    (0) TextBox is simply placed on the Form, WITHOUT Dock.
    (1) Compute the required size for the TextBox in your TextChanged() event.
    (2) If the required size is larger than the current size then....
    (3) Compute the difference between the required size and the current size.
    (4) Increase the Height of the TextBox AND the Form by that computed difference.
    LVL 35

    Author Comment

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Don't you already have it line #9 above?

    Just store it in a variable:   * untested code *

        // ... rest of code before this ...
        int requiredHeight = this.textBox1.Font.Height * numLines + padding + border;
        if (requiredHeight > this.textBox1.Height)
            int difference = requiredHeight - this.textBox1.Height;
            this.textBox1.Height = this.textBox1.Height + difference; // increase textbox
            this.Height = this.Height + difference; // increase form

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now