Skip to content
Related Articles

Related Articles

Improve Article

C# | RichTextBox Class

  • Last Updated : 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:

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(); 
    
  • 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 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:

Constructor

ConstructorDescription
RichTextBox()This Constructors is used to initialize a new instance of the RichTextBox class.

Properties

PropertyDescription
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 color 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 color 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.



My Personal Notes arrow_drop_up
Recommended Articles
Page :