C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
BackColorTip: If there is no current selection, then the caret is used to start the selection.
Example that demonstrates RichTextBox: C#
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(" ");
}
}
}
}
}
Note: This following screenshot shows the RichTextBox with a ZoomFactor of 3. We see that the text is considerably larger.
However: If you do not add the LinkClicked event handler, you will not be able to act upon a click.
Here: We simply launch the user's default web browser by passing the clicked URL to Windows.
Example that uses DetectUrls and LinkedClicked: C#
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);
}
}
}
Output
Link clicked opens in user's default web browser.
And: In this case, the scrollbar is drawn in a disabled state and cannot be used.
Next: In this example, we assign the window's title text to the text in the RichTextBox.
Example that uses TextChanged: C#
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;
}
}
}
Also: You can use the SelectedText property to change whatever text is selected, or to find out what text is currently selected.
Example that uses Select and SelectedText: C#
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("The Dev Codes");
richTextBox1.Select(4, 3);
this.Text = richTextBox1.SelectedText;
}
}
}
Output
1. The characters "Net" are selected.
2. The window text is set to Net.
And: Parallel to this, Paste will replicate the data from the system clipboard into the RichTextBox.
However: If the box is used to create formatted text documents, such as for posting to a website, then the RichTextBox is better.