RichTextBox

• A rich textbox is used for development of text editor applications like word pad, edit plus etc.
• It offers better features, when compared with the normal textbox.
• It supports built-in file interaction with “.rtf” files. (rtf stands for Rich Text Format).
• One of the highlights of rich textbox is, to support different fonts and colors for part of the text.

API: System.Windows.Forms.RichTextBox
Naming Convension: rtbxxxxxx
Run Time Properties of RichTextBox
Property Property
Text Gets or sets the text of the entire rich textbox.
SelectedText Gets or sets the currently selected text.
SelectionBackColor Represents the background color for the selected text.
SelectionColor Represents the foreground color for the selected text.
SelectionAlignment Left / Right / Center / Justify
SelectionFont Represents the font settings for the seleted text.
Name, BackColor, ForeColor, Enabled, Visible, Size, Location, ContextMenuStrip, Scrollbars, BorderStyle

Events of RichTextBox
Event Description
TextChanged, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave
Property Description
Clear() Clears entire text of the control.
Cut() Cuts the selected text.
Copy() Copies the selected text.
Paste() Pastes the text from the clipboard.
SelectAll() Selects the entire text in the control.
LoadFile(“rtf file path”) Loads the text from the specified “.rtf” file.
SaveFile(“rtf file path”) Saves the text of the control, into “.rtf” file.
Undo() Undos the previous action.
Redo() Redos the previous action.

Demo on RichTextBox

Rich text Box
Design
RichTextBox: rtbMyText

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.Clear();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Reset();
openFileDialog1.Filter = "Rich Text Files|*.rtf";
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName != "")
rtbMyText.LoadFile(openFileDialog1.FileName);
}

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Reset();
saveFileDialog1.Filter = "Rich Text Files|*.rtf";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
rtbMyText.SaveFile(saveFileDialog1.FileName);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.Undo();
}

private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.Redo();
}

private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.Cut();
}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.Copy();
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.Paste();
}

private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.SelectAll();
}

private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.SelectedText = "";
}

private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
rtbMyText.SelectionFont = fontDialog1.Font;
}
private void backgroudColorToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
rtbMyText.SelectionBackColor = colorDialog1.Color;
}

private void foregroundColorToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
rtbMyText.SelectionColor = colorDialog1.Color;
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.Redo();
}

private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.Cut();
}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.Copy();
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.Paste();
}

private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.SelectAll();
}

private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
rtbMyText.SelectedText = "";
}

private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
rtbMyText.SelectionFont = fontDialog1.Font;
}

private void backgroudColorToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
rtbMyText.SelectionBackColor = colorDialog1.Color;
}

private void foregroundColorToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
rtbMyText.SelectionColor = colorDialog1.Color;
}