Open In App

C# - RichTextBox Class

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, the RichTextBox control provides rich text editing controls, advanced formatting features, and loading rich text format (RTF) files. In other words, RichTextBox controls allow displaying or editing flow content, including paragraphs, images, tables, etc. The RichTextBox class in C# is used to represent the Windows rich text box and also provides different types of properties, methods, and events. It is defined under System.Windows.Forms namespace.

But it has some limitations, such as lacking the 64K character capacity limit provided by the TextBox control. It is used to provide text manipulation and display features similar to word processing applications like Microsoft Word.

Ways to Create a RichTextBox In Windows Forms

There are primarily two ways to create a RichTextBox in Windows Forms, which are mentioned below.

  • Drag and drop (Design-Time)
  • Custom RichTextBox (Run-Time)

1. Drag and drop (Design-Time)

This is the easiest way to create a RichTextBox in Windows Forms using Visual Studio we just have to open the toolbox and drag and drop the RichTextBox on the form in the designer and further we can change the appearance of the RichTextBox using the properties. Follow these steps to create a RichTextBox.

Step 1: Now locate the project with the name. Here, we are using the default name which is Form1 and it will open a form in the editor that we can further modify.

Empth-forms

In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form 1.cs file for the custom logic.


Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.

ToolBox



Step 3. Now open the common controls and drag and drop the RichTextBox on the form where we want to it be placed.

RichTextBoxDragAndDrop


Step 4: Now right-click on the RichTextBox and select 'Properties' to open the Properties window in the Solution Explorer. And now we can change the appearance and the behaviour of the RichTextBox.

Properties

Now we can add different kinds of properties and change the appearance of the RichTextBox.


Output:

Output


2. Custom RichTextBox (Run Time)

In this method, we are going to modify the Form1.cs file and add custom code modification in C# with the help of the RichTextBox class. The following steps show how to create a RichTextBox dynamically:

Step 1: Create a RichTextBox control using the RichTextBox() constructor is provided by the RichTextBox class.

// Creating a RichTextBox control

RichTextBox box = new RichTextBox();


Step 2: After creating a RichTextBox control, set the property of the RichTextBox control provided by the RichTextBox class.

// Setting the location

// of the RichTextBox

box.Location = new Point(236, 97);


// Setting the background

// color of the RichTextBox

box.BackColor = Color.Aqua;


// Setting the text

// in the RichTextBox

box.Text = "!..Welcome to GeeksforGeeks..!";


Step 3: And lastly add this RichTextBox control to the form using the following statement:

// Adding this RichTextBox

// in the form

this.Controls.Add(box);


Step 4: Now double-click on the form in Design and it will open the Form1.cs file where code is written in C#. Here the program file is Form 1.cs Now write this code in Form1.cs file

Form1.cs file:

C#
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the 
            // properties of the label 
            Label lb = new Label();
            lb.Location = new Point(251, 70);
            lb.Text = "Enter Text";

            // Adding this label in the form 
            this.Controls.Add(lb);

            // Creating and setting the 
            // properties of the RichTextBox 
            RichTextBox box = new RichTextBox();
            box.Location = new Point(236, 97);
            box.BackColor = Color.LightGray;
            box.Text = "!..Welcome to GeeksforGeeks..!";

            // Adding this RichTextBox in the form 
            this.Controls.Add(box);

        }

    }
}


Output:

Output


Constructor

This class has one constructor, with the help of which we can create objects of this class in different ways. The constructor of this class is listed below:

1. RichTextBox(): This constructor is used to initialize a new instance of the RichTextBox class.

Syntax:

RichTextBox()


Properties

The properties of this class is listed below:

PropertiesDescription
AutoSizeThis property is used to get or set a value that indicates whether the control resizes based on its contents.
BackColorThis property is used to get or set the background colour for the control.
BorderStyleThis property indicates the border style for the control.
FontThis property is used to get or set the font of the text displayed by the control.
ForeColorThis property is used to get or set the foreground colour of the control.
HeightThis property is used to get or set the height of the control.
LocationThis property is used to get or set the coordinates of the upper-left corner of the RichTextBox control relative to the upper-left corner of its form.
NameThis property is used to get or set the name of the control.
TabStopThis property is used to get or set a value that shows whether the user can press the TAB key to provide the focus to the NumericUpDown.
SizeThis property is used to get or set the height and width of the control.
TextThis property is used to get or set the text to be displayed in the RichTextBox control.
VisibleThis property is used to get or set a value indicating whether the control and all its child controls are displayed.
WidthThis property is used to get or set the width of the control.
ZoomFactorThis property is used to get or set the current zoom level of the RichTextBox.
ShowSelectionMarginThis property is used to get or set a value indicating whether a selection margin is displayed in the RichTextBox.
SelectionTabsThis property is used to get or set the absolute tab stop positions in a RichTextBox control.
SelectedTextThis property is used to get or set the selected text within the RichTextBox.
ScrollBarsThis property is used to get or set the type of scroll bars to display in the RichTextBox control.
MultilineThis property is used to get or set a value indicating whether this is a multiline RichTextBox control.


Real- World Applications and Challenges

It is commonly used in applications that require text formatting such as word processors, email clients, and text editors. It provides us the ability to add features like text styling, choose different fonts, and even inserting images.

Limitations of RichTextBox:

The limitations of RichTextBox are listed below:

  • The TextBox control has a 64K character limit but the RichTextBox does not have this limit and if when we work with very large file then the performace also decreases.
  • The RichTextBox does not support modern features like emojis, video and audio recording, editing, etc.
  • It does not provide advance features like inline editing and real time spell checking.

Similar Reads