• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 734
  • Last Modified:

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

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?
  • 3
  • 2
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
YZlatAuthor Commented:
(1) and (2) are correct,

form cannot be resized by the user and neither textbox nor form can shrink
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
YZlatAuthor Commented:
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now