Table of contents
1.
Introduction
1.1.
Design-Time
1.2.
Run -Time
2.
Frequently Asked Questions
2.1.
What is the difference between TextBox control and RichTextBox control?
2.2.
What is the file format exclusively used in RichTextBox control?
2.3.
What is the difference between TextBox and RichTextBox controls?
2.4.
What is RichTextBox?
2.5.
When would it be appropriate to use a rich TextBox instead of TextBox?
3.
Conclusion
Last Updated: Mar 27, 2024

C# RichTextBox Class

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

RichTextBox is a textbox in C# that provides rich text editing tools and extensive formatting features, as well as the ability to load rich text format (RTF) files. RichTextBox controls, in other words, allow you to view or alter flow content such as paragraphs, photos, tables, and so on. The RichTextBox class is generally used to represent the rich text box in Windows, as well as to provide many attributes, methods, and events. The namespace System.Windows.Forms contain their definition.

It does not have the same limit of 64K characters as the TextBox control. It's used to manipulate and display text in a similar way to word processing programs like Microsoft Word. In C#, there are two methods for creating a RichTextBox in a Windows form:

Recommended Topic, Palindrome in C#, singleton design pattern in c# and Ienumerable vs Iqueryable

Design-Time

This one is the easiest way to create a RichTextBox. Following are the steps to do it. The first step will be to create a form windows as shown in the following image:

To do that in Visual Studio, you will have to click on the File menu, then go to New. After that click on Project, then WindowsFormApp. 

In the second step, we will drag and drop the RichTextBox Control available in the toolbox to the form.

 

 

After successful drag and drop, the properties of the RichTextBox control can be modified easily as per your requirements. 

Output:

 

Run -Time

In this method, you can create a RichTextBox control using a program with the help of syntax provided by the RichTextBox class.

Following are the steps to create a RichTextBox control dynamically; The RichTextBox class provides the RichTextBox() constructor for creating a RichTextBox control.

// To create a RichTextBox Control
RichTextBox box = new RichTextBox();

 

After we have successfully created the RichBox control, we have to set the property as per our requirement, of the RichTextBox control provided by the RichTextBox class.

// To set the location of the RichTextBox
box.Location = new Point(236, 97); 

// To set the background color of the RichTextBox
box.BackColor = Color.Aqua; 

// Setting the text in the RichTextBox
box.Text = "!..Welcome to Coding Ninjas..!";

 

The final step is to add the created RichTextBox control to the form. This can be done by:

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

 

Let us see an 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 Coding Ninjas..!";
  
        // Adding this RichTextBox in the form
        this.Controls.Add(box);
    }
}
}

 

Output:

Frequently Asked Questions

What is the difference between TextBox control and RichTextBox control?

The RichTextBox is similar to the TextBox, but it has additional formatting capabilities. Whereas the TextBox control allows the Font property to be set for the entire control, the RichTextBox allows you to set the Font, as well as other formatting properties, for selections within the text displayed in the control.

What is the file format exclusively used in RichTextBox control?

With the RichTextBox control, the user can enter and edit text. The control also provides more advanced formatting features than the standard TextBox control. Text can be assigned directly to the control, or can be loaded from a rich text format (RTF) or plain text file.

What is the difference between TextBox and RichTextBox controls?

The Key Difference Between Textbox and Rich Textbox is that Textbox is used to display a single line input box. Rich Textbox is meanly used to display more than one line input.

What is RichTextBox?

The RichTextBox control enables you to display or edit flow content including paragraphs, images, tables, and more. This topic introduces the TextBox class and provides examples of how to use it in both Extensible Application Markup Language (XAML) and C#.

When would it be appropriate to use a rich TextBox instead of TextBox?

A TextBox requires less system resources then a RichTextBox and it is ideal when only plain text needs to be edited (i.e. usage in forms). A RichTextBox mainly used if you want more control over styling the text color, type, font, alignment etc. So anything you can do in Microsoft Word, you can do with a RichTextBox.

Conclusion

In this article, we have studied RichTextBox class in C#. We have also discussed the different ways to use it in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding RichTextBox class in C# and if you would like to learn more, check out our articles on C# GenericsC# Variables and C# Reflection.

Recommended Reading:

Difference Between Analog and Digital Computer

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available, Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass