articles

Home / DeveloperSection / Articles / RichTextBox Control in C#.Net

RichTextBox Control in C#.Net

RichTextBox Control in C#.Net

Anonymous User 21552 24-Jan-2011

RichTextBox Control in C#.Net

WhereThe RichTextBox control enables text to be displayed with formatting in plain text or rich-text format (RTF).

It also provides the facility to save the text in the file and restore it from a file.

Drag and drop RichTextBox control from the toolbox on the window Form.

RichTextBox Control in C#.Net

Write the Text in RichTextBox

private void frmRichTextBoLoad(object sender, EventArgs e)
{
            // write textin RichTextBox
            richTextBox1.Text= "Hello How are you";
}

RichTextBox Control in C#.Net

Change ForeColor of text written in RichTextBox

In RichTextBox each text can be in a different color.

Code:
private void frmRichTextBoLoad(object sender, EventArgs e)
        {
            //Find method is used to find the text written in RichTextBox
            richTextBox1.SelectionStart= richTextBox1.Find("Hello");
            richTextBox1.SelectionColor= Color.Red;
            richTextBox1.SelectionStart= richTextBox1.Find("How are you");
            richTextBox1.SelectionColor= Color.RoyalBlue;
         }


Run the Project

RichTextBox Control in C#.Net

Save RichTextBox Text into File and Restore from File

Code:

Write code in the click event of the button. When you double click on click event then click event handler will create in .cs file.

RichTextBox Control in C#.Net

private void btnSave_Click(object sender, EventArgs e)
{
     //SaveFile Method Saves the contents of the RichTextBox into a file
     richTextBox1.SaveFile("h1.rtf");
}
private void btnLoad_Click(object sender, EventArgs e)
{
  //LoadFile Method Loads the contents of a file into the RichTextBox control
     richTextBox2.LoadFile("h1.rtf");
}

RichTextBox Control in C#.Net

When you click save button then text written in RichTextBox1 will save to the specified file.

When you click the Load button then contents of a specified file Load into the RichTextBox2.

RichTextBox Control in C#.Net


Updated 14-Jul-2020
I am a content writter !

Leave Comment

Comments

Liked By