Resize a RichTextBox to fit its contents in C#

[Resize a RichTextBox]

This example shows how you can resize a RichTextBox control so it fits the text and images that it contains.

The example Resize a TextBox to fit its text in C# shows how to make a TextBox fit its text. That example uses a TextRenderer to determine how big the control’s text will be when it is drawn on the screen and then resizes the control accordingly.

That approach won’t work with a RichTextBox because, unlike a TextBox, a RichTextBox can contain text in different fonts, text that is aligned in various ways, and even pictures.

When this example starts, the following code executes to prepare the RichTextBox.

// Don't let the RichTextBox wrap long lines.
private void Form1_Load(object sender, EventArgs e)
    rchContents.WordWrap = false;
    rchContents.ScrollBars = RichTextBoxScrollBars.None;

This code sets the control’s WordWrap property to false so the control doesn’t try to wrap long lines of text to fit its current size. If you allow the control to wrap text, you get strange results where the control wraps text and then tries to make itself narrower. If you add a bunch of text, the control eventually makes itself extremely tall and thin. There may be a way to make this work but it doesn’t seem useful.

Next, this code sets the control’s ScrollBars property to None. If you don’t do this, the control may display scroll bars when its size is close to the size required by the contents. The scroll bars take up a lot of room (relatively speaking) so you can’t see all of the contents. You can add some extra space to make sure the text fits (in fact, the code that follows does this to make things look nicer), but the scroll bars mess things up if they appear so you may as well turn them off.

When the contents of the control change and the new contents require a different amount of space than the old contents, the RichTextBox raises its ContentsResized event and the following event handler executes.

// Make the RichTextBox fit its contents.
private void rchContents_ContentsResized(object sender,
    ContentsResizedEventArgs e)
    const int margin = 5;
    RichTextBox rch = sender as RichTextBox;
    rch.ClientSize = new Size(
        e.NewRectangle.Width + margin,
        e.NewRectangle.Height + margin);

This code uses the e.NewRectangle parameter to see how much space the control’s new contents needs. It adds a margin so things don’t look too crowded and sets the control’s ClientSize.

Run the program and type some text into the RichTextBox. You can also copy images or formatted text (such as the colored text displayed in Visual Studio’s code editor) and paste it into the RichTextBox to see how it handles them.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in controls, user interface and tagged , , , , , , , , , , , . Bookmark the permalink.

10 Responses to Resize a RichTextBox to fit its contents in C#

  1. Friday Five-July 19, 2013

    1. Resize a RichTextBox to fit its contents in C# By Visual Basic MVP Rod Stephens 2. ADFS 2.1 Mex Endpoint

  2. Patrick says:

    Is there a way to do exactly what this code does only after loading the contents of an RTF file into the RichTextBox?

  3. RodStephens says:

    You should be able to do that. Just load the contents. This program’s ContentsResized event will fire and the event handler will resize the control automatically.

  4. Patrick says:

    Strange. An RTF I create in Word 2013 does not autosize the RichTextBox but an RTF created in Wordpad does. Opening both in Notepad revels that Word puts a lot more “stuff” in the RTF than Wordpad. If you feel like testing I would be interested if you can figure out why. If not, the code still gets me what I need now that I know how to work around it. Thanks. Great blog!

  5. RodStephens says:

    Huh. I’m not surprised that Word and WordPad generate different RTF. I am surprised the Word data doesn’t make the control raise its ContentsResized event. I can’t imagine why that would be.

        Great blog!

    Spread the word 😉

  6. king khan says:

    Hello dear
    how to get image of a richtext box in jpg or other formate i am using bitmap but can not getting the content of rich textbox. thanks

  7. ZABI M says:

    Thanks for this blog I got many solutions to my problem form here. KINDLY Need little help more from you with guidance “how to create a table in rich text editor ” which can be moveable in the rich textbox and its cells should be resizeable in run time same as we MS WORD TABLE. THANKS

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.