Home
Search
RichTextBox ExampleUse RichTextBox in Windows Forms. Set colors and invoke the AppendText method.
C#
This page was last reviewed on Oct 5, 2022.
RichTextBox has many formatting options. It applies colors, background colors, multiple fonts, and margins to the text. It adds more complexity than the regular TextBox.
Getting started. The RichTextBox provides needed features to programs. Please drag a RichTextBox to your Form from the ToolBox in Visual Studio.
TextBox
Color example. Here we generate colored text blocks. In the Form1_Load event, the Font is changed, and then the BackColor is set to a light shade of blue.
Next Two data arrays are looped over. We use SelectionBackColor and the AppendText method.
Info The SelectionBackColor property changes the currently selected text in the RichTextBox to have the background color you assign to it.
Tip If there is no current selection, then the caret is used to start the selection.
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { richTextBox1.Font = new Font("Consolas", 18f, FontStyle.Bold); richTextBox1.BackColor = Color.AliceBlue; string[] words = { "Dot", "Net", "Perls", "is", "a", "nice", "website." }; Color[] colors = { Color.Aqua, Color.CadetBlue, Color.Cornsilk, Color.Gold, Color.HotPink, Color.Lavender, Color.Moccasin }; for (int i = 0; i < words.Length; i++) { string word = words[i]; Color color = colors[i]; { richTextBox1.SelectionBackColor = color; richTextBox1.AppendText(word); richTextBox1.SelectionBackColor = Color.AliceBlue; richTextBox1.AppendText(" "); } } } } }
DetectUrls. Sometimes a link to a web page will be entered. If you set the DetectUrls property to true, the RichTextBox will add hyperlink styles to any possible link.
However If you do not add the LinkClicked event handler, you will not be able to act upon a click.
Here We launch the user's default web browser by passing the clicked URL to Windows.
using System.Diagnostics; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e) { Process.Start(e.LinkText); } } }
TextChanged. RichTextBox has a TextChanged event handler. Whenever the text changes for any reason in the RichTextBox, the code in the TextChanged event handler executes.
Next In this example, we assign the window's title text to the text in the RichTextBox.
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void richTextBox1_TextChanged(object sender, EventArgs e) { this.Text = richTextBox1.Text; } } }
Select, SelectedText. The RichTextBox also offers the ability to select text based on a starting index and a count of characters, with the Select method.
Also You can use the SelectedText property to change whatever text is selected, or to find out what text is currently selected.
using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { richTextBox1.AppendText("Dot Net Perls"); richTextBox1.Select(4, 3); this.Text = richTextBox1.SelectedText; } } }
ZoomFactor. One useful property on the RichTextBox control is the ZoomFactor property. You can set its default value in the Windows Forms designer.
AutoWordSelection. This can be True or False. When True, the selection when the user drags over some text will expand to contain the word being hovered over.
RightMargin. RightMargin is somewhat confusing to use and you will have to experiment. The goal is to have some whitespace on the right side of the enclosed text box space.
TextLength. In my testing, this property returned the same number as using richTextBox1.Text.Length. Often we can take the string length of the Text property.
Copy, Paste. You will find the Copy and Paste methods. There is also a CanPaste boolean method for testing if pasting is possible. Windows Forms programs use the system clipboard.
Rtf. RTF stands for Rich Text Format, and this describes a certain file type where text formatting is encoded with special characters embedded in the text.
Tip When you apply formatting in a RichTextBox, you can access the richTextBox1.Rtf property to get the encoded RTF data.
Summary. We can control the RichTextBox programmatically through C# code. And we can change many properties inside the Visual Studio designer before the program ever executes.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 5, 2022 (edit).
Home
© 2007-2022 sam allen.
see site info on the changelog.