Table in RichTextBox With Text Wrap in Columns (Cells)
Table in RichTextBox With Text Wrap in Columns (Cells)
Home Articles Tutorials Forums Career Advice Jobs .NET FAQs News Member Login
Columns(Cells)
The RichTextBox control permits us to display and edit content such as paragraphs,
images, and tables.In one of my earlier posts, How To Make Table in RichTextBox
Using C# I have shown samples for creating table in rich text box control.
However, there are still some limitations with entering long string values,paragraphs,
and images in table column/cell. Recent Posts
What Is
SetCompatibleTextRenderingDefault
(false)
demonstrate the same. Here we inherit the basic RichTextBox to provide the cell text
How to remove Header of the XML file in
wrap option. C#?
So instead of directly dropping the RichTextBox in your form, create the Extended What is DBMS?
The requirement here is very simple. We have to specialize the .NET Rich Textbox
control class as below.Here we will be making use of Richedit from ms edit.dll.
C#
1 using System;
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
4
5 namespace ExtendedRichTextBoxSample
6 {
7 class ExtendedRichTextBox : RichTextBox
8 {
9 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
10 static extern IntPtr LoadLibrary(string dllName);
11 protected override CreateParams CreateParams
12 {
13 get
14 {
15 CreateParams baseParams = base.CreateParams;
16 if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
17 {
18 baseParams.ClassName = "RICHEDIT50W";
19 }
20 return baseParams;
21 }
22 }
23 }
24 }
Use this extended rich textbox control instead of the basic RichTextBoxcontrol.
Continue code as below in your Form class.
On Click of Button Table will be created. Table creation steps are same as in previous
example, Create Table In RichTextBoxusing C#
C#
1 using System;
2 using System.Data;
3 using System.Drawing;
4 using System.Text;
5 using System.Windows.Forms;
6
7 namespace ExtendedRichTextBoxSample
8 {
9 public partial class Form1 : Form
10 {
11 publicForm1()
12 {
13 InitializeComponent();
14 }
15
16 // Create and Display the table in RichTextBOxon a button click.
17 // The ExtedndedRichTextBox control has the word wrap feature.
18 private void btnCreateTable_Click(object sender, EventArgs e)
19 {
20 //Instance of the customized RichTextBox control class.Create Instance
21 //Programatically.
22 ExtendedRichTextBox advRichTextBox = new ExtendedRichTextBox();
23 advRichTextBox.Size = new Size(400, 300);
24
25 //Uncomment this linerichTextBox.MaximumSize if you want to limit the size of
26 //rich textbox.Scollbar gets enabled automaticallywhen it crosses this maximum
27 //limit.
28 //richTextBox.MaximumSize = new Size(500, 400);
29 //Since too much string appending, go for string builder
30 StringBuilder strRtfTable = new StringBuilder();
31
32 //beginning of rich text format,dont customize this begining line
33 strRtfTable.Append(@"{\rtf1 ");
34 //create 5 rows with 3 cells each.Write a method with parameters for accepting any
35 no of rows and columns.
36 for (inti = 0; i< 5; i++)
37 {
38 strRtfTable.Append(@"\trowd");
39
40 strRtfTable.Append(@"\cellx1000");
41
42 strRtfTable.Append(@"\cellx2000");
43
44 //Last cell with width 2000.Last position is 4000 (which is 2000+2000)
45 strRtfTable.Append(@"\cellx4000");
46
47 strRtfTable.Append(@"\intbl \cell \row"); //create row
48 }
49
50 strRtfTable.Append(@"\pard");
51 strRtfTable.Append(@"}");
52 advRichTextBox.Rtf = strRtfTable.ToString();
53
54 //Add the programatically created richtextbox to the form's control collection.
55 //Else it won't be displayed in form.
56 this.Controls.Add(advRichTextBox);
57
58 //Bring the control to top level,else will be appearing in back.
59 advRichTextBox.BringToFront();
60 }
61 }
62 }
Summary:
This article will definitely help you in creating table in RichTextBox with word wrap in
tabe cells (richtextbox columns). Don’t forget to provide your valuable suggestions and
feedback in the comments below.
You may also be interested in article, Load and Display RTF file With Table
InRichTextBox control and
Related Posts:
How to add text in a created table in a richtextbox?
Display rtf file correctly In RichTextBox – C#
How To Create Table in RichTextbox Using C#
Comments
I have Applied Same Custom control as per above But still words are not wrapping for multiline text in one cell.
There are showing in one line and passing through cells in one row.
I am preparing form to display Data in Table Format dynamically which are retrieved from Database. Data may be
Multiline Text ,Date , etc.
I am finding solution for Same Results as you show on screenshot Can You please say what can be other property
which need to set.
Reply
Rajeev says
July 5, 2016 at 3:12 pm
This sample table in RichTextBox with text wrap in cells works perfectly as explained.You can enter lengthy text and
see, it will wrap automatically as per size of the cell.copy paste the code in your application and see again.
For dynamic population of data in RichTextBox Table from database , you check my another article
http://www.dotnetstu s.com/how-to-add-text-in-a-created-table-in-a-richtextbox/ , where in second example, I
am populating data from a DataTable.Use this extended RichTextBox class for wrapping.
Hope this will help you. Let me know if any doubt further.
Reply
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Post Comment