RichTextBox
This has many formatting options. It applies colors, background colors, multiple fonts, and margins to the text. It adds more complexity than the regular TextBox
.
The RichTextBox
provides needed features to programs. Please drag a RichTextBox
to your Form from the ToolBox
in Visual Studio.
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.
SelectionBackColor
and the AppendText
method.SelectionBackColor
property changes the currently selected text in the RichTextBox
to have the background color you assign to it.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.
LinkClicked
event handler, you will not be able to act upon a click.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.
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.
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.
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 stands for Rich Text Format, and this describes a certain file type where text formatting is encoded with special characters embedded in the text.
RichTextBox
, you can access the richTextBox1.Rtf
property to get the encoded RTF data.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.