A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer


Table in RichTextBox With Text Wrap In Columns(Cells)

There is limitation in Visual Studio.Net RichTextBox control that you can not enter lengthy text in the columns(cells) of the created table.The available wordwrap property of the RichTextBox is for the control not for the cells of the table.This article explains how you can enter lengthy texts in your table columns with automatic text wrap provision.

In my previous article How To Make Table in RichTextBox Using C# I explained the very basics of creating a table in C# and got very good response from my readers.But there is limitation that you can not enter lengthy text in the columns(cells) of the created table.This is due to the limitation in Rich TextBox available in .NET visual studio.So in this article I am explaining an approach where you can enter lengthy texts in your table columns with automatic text wrap provision.

You need to extend the current Rich Textbox control using a class as below.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace RichTextBoxSample
    class ExtendedRichTextBox : RichTextBox
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr LoadLibrary(string dllName);
        protected override CreateParams CreateParams
                CreateParams baseParams = base.CreateParams;
                if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
                    baseParams.ClassName = "RICHEDIT50W";
                return baseParams;

Now you can use this extended control class instead of the Richtextbox available in visual studio.Continue as explained below in your form class

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace RichTextBoxSample
    public partial class Form1 : Form
        public Form1()
        /// <summary>
        /// Display the table on a button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreateTable_Click(object sender, EventArgs e)
            //Instance of the customized control class
             ExtendedRichTextBox richTextBox = new ExtendedRichTextBox();
             richTextBox.Size = new Size(500, 400);

            //Uncomment this line if you want to limit the size of rich textbox.Scollbar gets enabled automatically 
             //when it crosses this maximum limit
             //richTextBox.MaximumSize = new Size(500, 400);           

            //Since too much string appending go for string builder
            StringBuilder tableRtf = new StringBuilder();           

            //beginning of rich text format,dont customize this begining line             
            tableRtf.Append(@"{\rtf1 ");          
	    //create 5 rows with 3 cells each.Write a method with parameters for accepting any no of rows and columns.
            for (int i = 0; i < 5; i++)
               //A cell with width 1000.

                //Another cell with width 2000.end point is 3000 (which is 1000+2000).

                //Another cell with width 1000.end point is 4000 (which is 3000+1000)

                tableRtf.Append(@"\intbl \cell \row"); //create row

            richTextBox.Rtf = tableRtf.ToString();

            //Add the created richtextbox to the form's control collection.Else it won't be displayed.

            //Bring the control to top level

 Hope this article will help you some way. Also you may be interested in article

Load and Display rtf file With Table In RichTextBox control 

C++ Ultimate Video Training - Best Course Ever icon

Comments (1) -

Arun Shinde
India Arun Shinde

Great examples - got me out of major issue due to default Richtext control. Thanks again!


Pingbacks and trackbacks (1)+

Add comment

  Country flag

  • Comment
  • Preview