Resize a RichTextBox to fit its contents in C#

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 too 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 resize 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 new contents need. 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 them into the RichTextBox to see how it handles them.



What did you think of this article?

  • 7/19/2013 11:14 AM The Microsoft MVP Award Program Blog wrote:
    1. Resize a RichTextBox to fit its contents in C# By Visual Basic MVP Rod Stephens 2. ADFS 2.1 Mex Endpoint
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.


 Email (will not be published)


Your comment is 0 characters limited to 3000 characters.