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.
The word wrap option was not possible in the RichTextBox control. This limits us from entering lengthy text strings in the created table cells. Entire table structure will get distorted .
To overcome this limitation, the basic richtextbox control needs to be extended to incorporate the feature of wrapping the cell text in next line.
I planned this article, Table in RichTextBoxWith Text Wrap InColumns (Cells), to demonstrate the same. Here we inherit the basic RichTextBox to provide the cell text wrap option.
So instead of directly dropping the RichTextBox in your form, create the Extended RichTextBox control instance programmatically. Later, you can extend it to a usercontrol with more options.
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 msftedit.dll.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ExtendedRichTextBoxSample
{
class ExtendedRichTextBox : RichTextBox
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr LoadLibrary(string dllName);
protected override CreateParams CreateParams
{
get
{
CreateParams baseParams = base.CreateParams;
if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
{
baseParams.ClassName = "RICHEDIT50W";
}
return baseParams;
}
}
}
}
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#
using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ExtendedRichTextBoxSample
{
public partial class Form1 : Form
{
publicForm1()
{
InitializeComponent();
}
// Create and Display the table in RichTextBOxon a button click.
// The ExtedndedRichTextBox control has the word wrap feature.
private void btnCreateTable_Click(object sender, EventArgs e)
{
//Instance of the customized RichTextBox control class.Create Instance
//Programatically.
ExtendedRichTextBox advRichTextBox = new ExtendedRichTextBox();
advRichTextBox.Size = new Size(400, 300);
//Uncomment this linerichTextBox.MaximumSize if you want to limit the size of
//rich textbox.Scollbar gets enabled automaticallywhen it crosses this maximum
//limit.
//richTextBox.MaximumSize = new Size(500, 400);
//Since too much string appending, go for string builder
StringBuilder strRtfTable = new StringBuilder();
//beginning of rich text format,dont customize this begining line
strRtfTable.Append(@"{\rtf1 ");
//create 5 rows with 3 cells each.Write a method with parameters for accepting any
no of rows and columns.
for (inti = 0; i< 5; i++)
{
strRtfTable.Append(@"\trowd");
strRtfTable.Append(@"\cellx1000");
strRtfTable.Append(@"\cellx2000");
//Last cell with width 2000.Last position is 4000 (which is 2000+2000)
strRtfTable.Append(@"\cellx4000");
strRtfTable.Append(@"\intbl \cell \row"); //create row
}
strRtfTable.Append(@"\pard");
strRtfTable.Append(@"}");
advRichTextBox.Rtf = strRtfTable.ToString();
//Add the programatically created richtextbox to the form's control collection.
//Else it won't be displayed in form.
this.Controls.Add(advRichTextBox);
//Bring the control to top level,else will be appearing in back.
advRichTextBox.BringToFront();
}
}
}
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
Bhavin Patel says
Thank you Sir ,
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.
Rajeev says
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.dotnetstuffs.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.