C# | RichTextBox Class - GeeksforGeeks

05 Sep, 2019
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. The RichTextBox class is used to represent the windows rich text box and also provide different types of properties, methods, and events. It is defined under System.Windows.Forms namespace.It does not have the same 64K character capacity limit like TextBox control. It is used to provide text manipulation and display features similar to word processing applications like Microsoft Word. In C# you can create a RichTextBox in the windows form by using two different ways:
1. Design-Time: It is the easiest way to create a RichTextBox as shown in the following steps:
Step 1: Create a windows form as shown in the below image:Visual Studio -> File -> New -> Project -> WindowsFormApp
Step 2: Next, drag and drop the RichTextBox control from the toolbox to the form.
Step 3: After drag and drop you will go to the properties of the RichTextBox control to modify RichTextBox according to your requirement.Output:
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a RichTextBox control programmatically with the help of syntax provided by the RichTextBox class. The following steps show how to set the create 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(); 

// 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..!"; 

// 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 last add this RichTextBox control to the form using the following statement:// Adding this RichTextBox
// in the form 
this.Controls.Add(box); 
Example:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;  namespace WindowsFormsApp30 {  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.Aqua;        box.Text = "!..Welcome to GeeksforGeeks..!";          // Adding this RichTextBox in the form        this.Controls.Add(box);    }}}Output:
// Adding this RichTextBox
// in the form 
this.Controls.Add(box); 

Example:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;  namespace WindowsFormsApp30 {  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.Aqua;        box.Text = "!..Welcome to GeeksforGeeks..!";          // Adding this RichTextBox in the form        this.Controls.Add(box);    }}}
Output:
CSharp-Windows-Forms-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
C# | Method Overriding
Destructors in C#
Difference between Ref and Out keywords in C#
C# | Delegates
C# | Constructors
Extension Method in C#
Introduction to .NET Framework
C# | Class and Object
C# | Abstract Classes