Professional Documents
Culture Documents
How To Create Table in RichTextbox Using C#
How To Create Table in RichTextbox Using C#
How To Create Table in RichTextbox Using C#
Home Articles Tutorials Forums Career Advice Jobs .NET FAQs News Member Login
Being a .NET programmer, you may have to edit formatted text, images, tables and any
other such rich content. With simple textbox control, it is not possible.Here comes the
use of Rich Text Box control.
Recent Posts
Rich Text Format (RTF) specifications enable to format text and graphics. This
formatting feature helps Rich Text Format to be used with di erent output devices, Serialize Object to XML in C# | Write
Object Data to XML File in C#
di erent operating systems, and operating environments.
What is the use of
RTF documents created with di erent so ware applications under di erent operating Application.EnableVisualStyles()?
systems can be transferred between those so ware applications and operating
XPATH With Default Namespace In C# –
systems. For a detailed understanding of Rich Text Format read Rich Text Format
XPathNavigator
Specification.The RichTextBox control permits the user to enter and edit text with
more advanced formatting features than the simple textbox control. String Value Enum with Space in between
– Description Attribute
we will try to clear your doubt. A er reading this article you will get a clear
understanding of how to insert Tables in RichTextBox control using C#.
For better understanding check the small sample given below. This example shows
Recent Forum Topics
how to create a table with 5 rows and 3 columns in RichTextbox control. You can
extend the number of rows and columns as per your need. What is the Di erence Between
IEnumerable and IQueryable in C#
In this example, we will be using a StringBuilder object for creating rows,cells, and How to remove Header of the XML file in
manipulating characters.Unlike simple string, StringBuilder normally contains a bu er, C#?
which can be manipulated(insert, append, remove, and replace) in place without
What is DBMS?
creating another new string.
A er we completely set the table string format, set this StringBuilder object to What is RDBMS?
RichTextBox control.
What is asp net framework ?
Here below you can see 2 examples. In the first example, we will create rows and cells
one by one. The second one will create the table in a loop by accepting parameters
passed through the method call. No of rows, columns and width of cell are passed as
parameters.You can choose whichever is most suitable for you.
Example 1:
I Created a new WinformProject.In Form, dragged and dropped a RichTextBox Control
and named it as richTextBox1(Set the rich text box width accordingly to accommodate
the table completely).
Added one button below the RichTextBox control and name it asbtnCreateTable.
C#
1 //Method to create a table format string which can directly be set to RichTextBox Control
2 private void InsertTableInRichtextbox()
3 {
4 //CreateStringBuilder object
5 StringBuilder strTable = new StringBuilder();
6
7 //Beginning of rich text format,don’t alter this line
8 strTable.Append(@"{\rtf1 ");
9
10 //Create 5 rows with 4 columns
11 for(inti = 0; i< 5; i++)
12 {
13 //Start the row
14 strTable.Append(@"\trowd");
15
16 //First cell with width 1000.
17 strTable.Append(@"\cellx1000");
18
19 //Second cell with width 1000.Ending point is 2000, which is 1000+1000.
20 strTable.Append(@"\cellx2000");
21
22 //Third cell with width 1000.Endingat3000,which is 2000+1000.
23 strTable.Append(@"\cellx3000");
24
25 //Last cell with width 1000.Ending at 4000 (which is 3000+1000)
26 sringTableRtf.Append(@"\cellx4000");
27
28 //Append the row in StringBuilder
29 strTable.Append(@"\intbl \cell \row"); //create the row
30 }
31
32 strTable.Append(@"\pard");
33
34 strTable.Append(@"}");
35
36 this.richTextBox1.Rtf = strTable.ToString();
37 }
Click of the button will create the table. In button click event Invoke the
InsertTableInRichTextBox function and assign the return string to the RichTextBox
control. Update button click event as below.
C#
1 private void btnCreateTable_Click(object sender, EventArgs e)
2 {
3 /*Insert Table in RichTextBox Control by setting .Rtf as the string returned.
4 Set the RichTextBox width to fit the table completely. */
5 this.richTextBox.Rtf = InsertTableInRichTextBox();
6 }
The above code will create a table with 5 rows and 3 columns in the rich text box
control.
Example 2:
In the same Form above change the functions as below,
C#
1 /* Method to create a table format string which can directly be set to RichTextBoxControl.Rows,
2 columns and cell width are passed as parameters rather than hard coding as in previous example.*
3 private static String InsertTableInRichTextBox(int rows, int cols, int width)
4 {
5 //Create StringBuilder Instance
6 StringBuilder sringTableRtf = new StringBuilder();
7
8 //beginning of rich text format
9 sringTableRtf.Append(@"{\rtf1 ");
10
11 //Variable for cell width
12 int cellWidth;
13
14 //Start row
15 sringTableRtf.Append(@"\trowd");
16
17 //Loop to create table string
18 for (inti = 0; i< rows; i++)
19 {
20 sringTableRtf.Append(@"\trowd");
21
22 for (int j = 0; j < cols; j++)
23 {
24 //Calculate cell end point for each cell
25 cellWidth = (j + 1) * width;
26
27 //A cell with width 1000 in each iteration.
28 sringTableRtf.Append(@"\cellx" + cellWidth.ToString());
29 }
30
31 //Append the row in StringBuilder
32 sringTableRtf.Append(@"\intbl \cell \row");
33 }
34 sringTableRtf.Append(@"\pard");
35 sringTableRtf.Append(@"}");
36
37 return sringTableRtf.ToString();
38 }
C#
1 private void btnCreateTable_Click(object sender, EventArgs e)
2 {
3 //Insert Table in RichTextBox Control by setting .Rtf as the string returned.
4 //Set the RichTextBox width to fit the table completely,
5 this.richTextBox.Rtf = InsertTableInRichTextBox(5,4,1000);
6 }
Summary
Don’t forget to provide you thoughts and suggestions in the comments section below.
If you know any better approach than this for creating a table in rich textbox control in
C#, please share it here.
Related Posts:
How to add text in a created table in a richtextbox?
Table in RichTextBox With Text Wrap In Columns(Cells)
Display rtf file correctly In RichTextBox – C#
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Post Comment