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.
Write the Text in RichTextBox
private void frmRichTextBoLoad(object sender, EventArgs e)
{
// write textin RichTextBox
richTextBox1.Text= "Hello How are you";
}
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
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.
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");
}
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.
Anonymous User
05-Mar-2019Very Nice Article.