MorkaLork Development

Interesting stuff I've picked up over the years...

RichTextBox

2009-05-16 18:41:27 | 464 views | rich text box richtextbox rtf ansi bold textbox

The basics


The RichTextBox is more or less like the normal textbox, but with the RichTextbox you can format the text in various ways.

Using the RichTextBox, or RTB from now on in this article, you can make a simple text editor. The text editor is what we will make in this article.

The text editor will look like this:

A simple text editor made with a RichTextBox


Getting into it


The text of a RTB can be changed by using the .Font property. Problem with this property is that you then format all the text in the RTB so what you want to use when creating a text editor is the .SelectionFont property.
This property will only change the currently highlighted text which is a lot more convenient for a text editor.

Create a form like the one in the picture and name the controls as follow (names will be self explanatory):

MainForm
txtBold
txtItalic
txtUnderlined
cmbFontFamily
cmbFontColor
cmbFontSize
myRichTextBox

First of all we need two Font objects, oldFont and newFont:



Font oldFont;
Font newFont;


After that we need a constructor that takes care of the comboboxes:



void MainFormLoad(object sender, EventArgs e)
{
//Load the font-families
cmbFontFamily.Items.Add("Arial");
cmbFontFamily.Items.Add("Sans Serif");
cmbFontFamily.Items.Add("Tahoma");
cmbFontFamily.Items.Add("Times New Roman");

//Load the font-sizes
cmbFontSize.Items.Add("8");
cmbFontSize.Items.Add("10");
cmbFontSize.Items.Add("12");
cmbFontSize.Items.Add("14");

//Load the font-sizes
cmbFontColor.Items.Add(Color.Black.Name);
cmbFontColor.Items.Add(Color.Aqua.Name);
cmbFontColor.Items.Add(Color.Green.Name);
cmbFontColor.Items.Add(Color.Red.Name);
}


The reason we use the Color.xxx.Name property isntead of just writing the name out is because we'll use the exact name to convert that text into a color object when changing the color.

When it comes to changing the text style (bold, italic and underline) we'll create a common method called changeFont(). This method will have one parameter, FontStyle fs.



private void changeFont(FontStyle fs)
{
//Get the current font
oldFont = myRichTextBox.SelectionFont;

//Create a new font
//Check if the current FontStyle property equals sent in FontStyle object or not
if(oldFont.Style.Equals((object)fs))
newFont = new Font(oldFont, oldFont.Style & ~fs); //Without the current style
else
newFont = new Font(oldFont, oldFont.Style | fs); //With the current style

//Set the new font
myRichTextBox.SelectionFont = newFont;
myRichTextBox.Focus();
}


This method creates a new font based on the old font but changes the current .Style property and sets it to either the new style or, if the style is already set, discards it. This is because you want the user to be able to, for example, select a bolded part and unbold it.
After creating the new font we apply that font to the selected text using the .SelectionFont property.

This method is called from each of the three styling buttons.
Example:



void BtnBoldClick(object sender, EventArgs e)
{
changeFont(FontStyle.Bold);
}


The comboboxes works in a similar ways (creating and new font and applying it to the selected text) with one exception, the Color settings.
When changing the colors we use the .SelectionColor property instead of the .SelectionFont to set the color.
We also use the ColorConverter class to convert the name of the color from the textbox into a propert Color object:



//Convert the text to a color object
ColorConverter conv = new ColorConverter();
Color color = (Color)conv.ConvertFromString(cmbFontColor.SelectedItem.ToString());

//Set the marked text to the correct color
myRichTextBox.SelectionColor = color;



This is how the text editor could look:

A working text editor created with a RichTextBox



The Full Code





/*
* Created by SharpDevelop.
* User: maffelu
* Date: 2009-05-16
* Time: 16:18
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace myRichTextBox
{
public partial class MainForm : Form
{
Font oldFont;
Font newFont;

public MainForm()
{
InitializeComponent();
}

void MainFormLoad(object sender, EventArgs e)
{
//Load the font-families
cmbFontFamily.Items.Add("Arial");
cmbFontFamily.Items.Add("Sans Serif");
cmbFontFamily.Items.Add("Tahoma");
cmbFontFamily.Items.Add("Times New Roman");

//Load the font-sizes
cmbFontSize.Items.Add("8");
cmbFontSize.Items.Add("10");
cmbFontSize.Items.Add("12");
cmbFontSize.Items.Add("14");

//Load the font-sizes
cmbFontColor.Items.Add(Color.Black.Name);
cmbFontColor.Items.Add(Color.Aqua.Name);
cmbFontColor.Items.Add(Color.Green.Name);
cmbFontColor.Items.Add(Color.Red.Name);
}

void BtnBoldClick(object sender, EventArgs e)
{
changeFont(FontStyle.Bold);
}

void BtnItalicClick(object sender, EventArgs e)
{
changeFont(FontStyle.Italic);
}

void BtnUnderlinedClick(object sender, EventArgs e)
{
changeFont(FontStyle.Underline);
}

private void changeFont(FontStyle fs)
{
//Get the current font
oldFont = myRichTextBox.SelectionFont;

//Create a new font
//Check if the current FontStyle property equals sent in FontStyle object or not
if(oldFont.Style.Equals((object)fs))
newFont = new Font(oldFont, oldFont.Style & ~fs); //Without the current style
else
newFont = new Font(oldFont, oldFont.Style | fs); //With the current style

//Set the new font
myRichTextBox.SelectionFont = newFont;
myRichTextBox.Focus();
}



void CmbFontFamilySelectedIndexChanged(object sender, EventArgs e)
{
//Get the current font
oldFont = myRichTextBox.SelectionFont;

//Create a new font
newFont = new Font(cmbFontFamily.SelectedItem.ToString(), oldFont.Size, oldFont.Style);

//Set the new font
myRichTextBox.SelectionFont = newFont;
myRichTextBox.Focus();
}

void CmbFontColorSelectedIndexChanged(object sender, EventArgs e)
{
//Convert the text to a color object
ColorConverter conv = new ColorConverter();
Color color = (Color)conv.ConvertFromString(cmbFontColor.SelectedItem.ToString());

//Set the marked text to the correct color
myRichTextBox.SelectionColor = color;
}

void CmbFontSizeSelectedIndexChanged(object sender, EventArgs e)
{
oldFont = myRichTextBox.SelectionFont;

newFont = new Font(oldFont.FontFamily, float.Parse(cmbFontSize.SelectedItem.ToString()), oldFont.Style);

myRichTextBox.SelectionFont = newFont;
myRichTextBox.Focus();

}
}
}


The Source Code





Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.