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.
|
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.
|
The final step is to add the created RichTextBox control to the form. This can be done by:
|
Let us see an example:
|
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.