TechyMedia

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

NAVIGATION - SEARCH

How To Make Table in RichTextbox Using C#

Is it possible To Create Table in RichTextBox?

RichTextBox control is always the best choice when it is required for the user to edit formatted text, images, tables and any other such rich contents wich is not possible in simple textbox. RTF (Rich Text Format) Specification provides  formats for text and graphics that can be used with different output devices, operating environments, and operating systems.

RTF documents created with different software applications under different operating systems can be transferred between those software applications and operating systems .

For understanding the possibilities of Rich textbox better  try the small sample below which create table in RichTextbox with 5 rows and 3 columns.

//Method to create C# richtextbox table

private void SetTableToRichtextbox()
  {
           //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

            for (int i = 0; i < 5; i++)
            {

                tableRtf.Append(@"\trowd");

               //A cell with width 1000.
                tableRtf.Append(@"\cellx1000"); 

                //Another cell with width 2000.end point is 3000 (which is 1000+2000).
                tableRtf.Append(@"\cellx3000"); 

                //Another cell with width 1000.end point is 4000 (which is 3000+1000)
                tableRtf.Append(@"\cellx4000");

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

            }

            tableRtf.Append(@"\pard");

            tableRtf.Append(@"}");

            this.richTextBox1.Rtf = tableRtf.ToString(); 
  }
 

The above mentioned table creation has a limitation that you can not enter lengthy text in columns which is a limitation of the .NET rich texbox control.So I have written another article recently to overcome this limitation which allows auto text wrap in table columns.See the  article

Table in RichTexBox With Text Wrap In Columns

Happy Coding!

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading