Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C#
Article

RicherTextBox

Rate me:
Please Sign up or sign in to vote.
4.82/5 (19 votes)
17 Mar 2008CPOL3 min read 137.7K   14K   90   29
User control for .NET 2.0 extending RichTextBox with various formatting options
Image 1

Introduction

The RicherTextBox .NET user control is an extension of the RichTextBox control. It has integrated toolstrips with the most common formatting options - choosing fonts, font style (bold, underline, italic), text alignment, bullets, indentation, zooming in and out and searching and replacing. Each one of the tools in the toolstrip can be hidden or shown in design-time or by using the code. In addition to the toolstrip, most of the functionality is also combined in a context menu. It also contains public methods for managing the format using code when the usage of the toolstrip is not appropriate. Save and load funcionality is also implemented. The RicherTextBox control is ready-to-use and its basic usage is as easy as dragging it from the toolbox into the container.

The toolstrip of RicherTextBox:

Sample Image - maximum width is 600 pixels

The context menu of RicherTextBox:

Sample Image - maximum width is 600 pixels

Using the Code

This control should be used as any other user control - just drag-drop in a designer, or create a new instance using a code editor. The only thing you need to do is to add a reference to the DLL-file of the control or the whole project in your project's references and add a using directive in your code file to the namespace RicherTextBox.

C#
// Include RicherTextBox namespace to be able to access the control's 
// properties and methods
using RicherTextBox;

Without any other code, the control will became available in its full capabilities. If you want to enable or disable some functionality, it can be done in design time using the properties in Toolstrip items visibility and Toolstrip single items visibility categories, or by setting them using C# code.

Here are some examples of hiding items in the toolstrip using code. Let's assume that the control is implemented in a form and its name is set to richerTextBox1 .

Hiding a group of tools:

C#
//
// To hide the group of buttons for text alignment using the properties
//
richerTextBox1.GroupAlignmentVisible = false;
//
// To hide the group of buttons for text alignment using the methods
//
richerTextBox1.HideToolstripItemsByGroup
    (RicherTextBox.RicherTextBoxToolStripGroups.Alignment, false);

The method HideToolstripItemsByGroup is declared as follows...

C#
public void HideToolstripItemsByGroup
   (RicherTextBoxToolStripGroups group, bool visible)

... where RicherTextBoxToolStripGroups is enumeration including the values:

C#
public enum RicherTextBoxToolStripGroups
{
    SaveAndLoad = 0x1,
    FontNameAndSize = 0x2,
    BoldUnderlineItalic = 0x4,
    Alignment = 0x8,
    FontColor = 0x10,
    IndentationAndBullets = 0x20,
    Insert = 0x40,
    Zoom = 0x80
}

The values in HideToolstripItemsByGroup's first parameter can be combined using the bitwise or operator (|):

C#
// Hiding more than one group of tools using a combination of enumerable values 
// as a parameter
richerTextBox1.HideToolstripItemsByGroup(
    RicherTextBoxToolStripGroups.Alignment |
    RicherTextBoxToolStripGroups.BoldUnderlineItalic,
    false);

This is what the control looks like after executing this code:

Sample Image - maximum width is 600 pixels

As you can see, the alignment buttons and the bold, italic, underline buttons are now hidden.

Using the Methods for Accessing Functionality (Examples)

Let's say you want to toggle the bold parameter of the selected text from the code, it is done in a very simple way:

C#
// Toggling Bold on the selected text
richerTextBox1.ToggleBold();    

Setting the size of the used font:

C#
// Setting the font size
richerTextBox1.SetFontSize(8.25f);

The same is used for all of the formatting functionality of the control.

If you don't want the save and load buttons visible (or, even if they're visible), you will probably want to access the text which the user has typed in the control. Two properties are supplied for this purpose:

  • Text is an overriding property which contains the text in the RicherTextBox in plain-text format.
  • Rtf holds the text in rich-text format.

Both properties can be set and read, and the Rtf property also has simple error protection: when a string with invalid format is associated with it, instead of throwing ArgumentException, the invalid text is simply set as plain-text format:

C#
// Setting the text
richTextBox1.Text = "This is an example of setting the text in a RicherTextBox";

// Now, an example of setting the Rtf property to an invalid value:
// The value "Test" is an invalid rich-text format string, but the RicherTextBox
// control will simply read it as plain-text instead of throwing exceptions
richTextBox1.Rtf = "Test";    

Searching and Replacing

All that you need is to show (or, not hide) the toolstrip for finding. All functionality of searching and replacing is integrated in the control.

Sample Image - maximum width is 600 pixels

History

  • Version 1.0 - 16 March, 2008: First released version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Interconsult Bulgaria
Bulgaria Bulgaria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
ABJ Bahadır Back12-Nov-21 6:40
ABJ Bahadır Back12-Nov-21 6:40 
GeneralMy vote of 5 Pin
Member 381004229-Nov-19 9:24
Member 381004229-Nov-19 9:24 
QuestionOpening a string - fails Pin
Member 1298335819-Apr-17 23:00
Member 1298335819-Apr-17 23:00 
Questionabraham Pin
Abraham Molina12-Oct-16 9:34
Abraham Molina12-Oct-16 9:34 
QuestionNeed additional properties Pin
Srikanth Anandateertha18-Oct-13 8:42
Srikanth Anandateertha18-Oct-13 8:42 
QuestionTextChanged Pin
AdrianoG30-Jul-13 3:27
AdrianoG30-Jul-13 3:27 
AnswerRe: TextChanged Pin
Simon Jeffs10-Jun-20 2:34
Simon Jeffs10-Jun-20 2:34 
QuestionUse on Multiple RichTextBoxes? Pin
trantrum27-May-13 2:13
professionaltrantrum27-May-13 2:13 
QuestionRead-Only Text Pin
Elie Mouawad7-Jan-13 3:37
Elie Mouawad7-Jan-13 3:37 
AnswerRe: Read-Only Text Pin
trantrum27-May-13 1:35
professionaltrantrum27-May-13 1:35 
QuestionHow to get the selected text Pin
The_F1-May-12 23:58
The_F1-May-12 23:58 
QuestionProblem with adding multiple instance of RicherTextBox to a form Pin
FernandoUY6-Mar-12 4:46
professionalFernandoUY6-Mar-12 4:46 
AnswerRe: Problem with adding multiple instance of RicherTextBox to a form Pin
FernandoUY6-Mar-12 4:53
professionalFernandoUY6-Mar-12 4:53 
Questionhow to give html input? Pin
Joyalg16-Feb-10 2:21
Joyalg16-Feb-10 2:21 
GeneralI added a ValueChanged event to the control... Pin
Steve McKenna1-Feb-10 6:42
Steve McKenna1-Feb-10 6:42 
Questionis it possible to open a specific *.rtf file by parameter ? Pin
Bernd Pfeiff6-Dec-09 3:47
Bernd Pfeiff6-Dec-09 3:47 
QuestionHow to numbering a line Pin
laduc1-Dec-09 15:35
laduc1-Dec-09 15:35 
Generalsave RicherTextBox Content to DB Pin
Bernd Pfeiff3-Sep-09 6:39
Bernd Pfeiff3-Sep-09 6:39 
QuestionHtml export Pin
Member 351717930-May-08 5:01
Member 351717930-May-08 5:01 
Generalmultiple font attributes Pin
Gary O'Neal25-Mar-08 4:54
Gary O'Neal25-Mar-08 4:54 
QuestionTables? Pin
georani25-Mar-08 2:48
georani25-Mar-08 2:48 
Great article.

The WordPad can open texts with tables, but it can not create tables.

My suggestion to you:
Add the ability to insert and edit tables.

Is this very difficult? Or it is impossible?
GeneralGood control - perhaps the article needs a bit extra... Pin
robvon23-Mar-08 23:04
robvon23-Mar-08 23:04 
QuestionVS 2005 version available ? Pin
BillWoodruff22-Mar-08 4:26
professionalBillWoodruff22-Mar-08 4:26 
AnswerRe: VS 2005 version available ? Pin
Célio30-Mar-08 8:08
Célio30-Mar-08 8:08 
Generalfao Celio Re: VS 2005 version available ? Pin
BillWoodruff30-Mar-08 19:50
professionalBillWoodruff30-Mar-08 19:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.