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.

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.

Step 3. Now open the common controls and drag and drop the RichTextBox on the form where we want to it be placed.
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.
Now we can add different kinds of properties and change the appearance of the RichTextBox.
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:
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:
Properties | Description |
---|
AutoSize | This property is used to get or set a value that indicates whether the control resizes based on its contents. |
BackColor | This property is used to get or set the background colour for the control. |
BorderStyle | This property indicates the border style for the control. |
Font | This property is used to get or set the font of the text displayed by the control. |
ForeColor | This property is used to get or set the foreground colour of the control. |
Height | This property is used to get or set the height of the control. |
Location | This 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. |
Name | This property is used to get or set the name of the control. |
TabStop | This 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. |
Size | This property is used to get or set the height and width of the control. |
Text | This property is used to get or set the text to be displayed in the RichTextBox control. |
Visible | This property is used to get or set a value indicating whether the control and all its child controls are displayed. |
Width | This property is used to get or set the width of the control. |
ZoomFactor | This property is used to get or set the current zoom level of the RichTextBox. |
ShowSelectionMargin | This property is used to get or set a value indicating whether a selection margin is displayed in the RichTextBox. |
SelectionTabs | This property is used to get or set the absolute tab stop positions in a RichTextBox control. |
SelectedText | This property is used to get or set the selected text within the RichTextBox. |
ScrollBars | This property is used to get or set the type of scroll bars to display in the RichTextBox control. |
Multiline | This 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
C# | RichTextBox Class
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
6 min read
C# | MaskedTextBox Class
In C#, the MaskedTextBox class control provides a validation procedure for the user input on the form, like date, phone numbers, etc. It provides a mask to enforce valid input formats. The MaskedTextBox class in C# is used to represent the Windows masked text box and also provides different types of
7 min read
C# TextBox Controls
In Windows forms, TextBox plays an important role. With the help of TextBox, the user can enter data in the application, it can be of a single line or multiple lines. The TextBox is a class and it is defined under System.Windows.Forms namespace.Ways to Create a TextBox In Windows FormsThere are main
5 min read
C# String Class
In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. The array of characters is also termed as the text. So the string is the representation of the text. A string is represented by a class System.String. The String c
9 min read
C# Console Class
In C#, the Console class is used to represent the standard input, output, and error streams for console applications. It is defined under the System namespace and does not contain any constructors. Instead of a constructor, this class provides various properties and methods to perform different oper
5 min read
C# - FileInfo Class Methods
In this article, we will explain the FileInfo class, its methods, properties, etc. The System.IO namespace is one of the most important namespaces we used while we are working with files in C#. FileInfo Class:It does not have static methods and it can only be used on instantiated objects. The class
3 min read
C# Class and Objects
Class and Object are the basic concepts of Object-Oriented Programming which revolve around real-life entities. A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member functions which define actions) into a single uni
5 min read
C# - Char Struct
In C#, the Char struct is used to represent a single Unicode character as a UTF-16 code unit, defined under the System namespace. A Char in C# is a 16-bit value, and it can represent characters in the Basic Multilingual Plane (BMP). Characters beyond 0xFFFF are represented by surrogate pairs (two Ch
4 min read
How to Add Text in the RichTextBox in C#?
In C#, RichTextBox control is a textbox which gives you rich text editing controls and advanced formatting features also includes a loading rich text format (RTF) files. Or in other words, RichTextBox controls allows you to display or edit flow content, including paragraphs, images, tables, etc. In
3 min read
How to set the Location of the RichTextBox in C#?
In C#, RichTextBox control is a textbox which gives you rich text editing controls and advanced formatting features also includes a loading rich text format (RTF) files. Or in other words, RichTextBox controls allows you to display or edit flow content, including paragraphs, images, tables, etc. In
3 min read