Formatting in Richtextbox
In this article, I will explain how to do basic format in richtextbox control of the windows application. The basic formats like Bold, italic, Underline, Strikeout are explained in this article and also explained setting shortcutkey for richtextbox.
Formatting in Richtextbox
By using electionFont property of richTextBox we can change format of the richtextbox. The basic formats like Bold, italic, Underline, Strikeout are set by fontstyle property of system.Drawing namespace. Using KeyDown event of the richtextbox shortcutkey can be set for the richtextbox.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((Control.ModifierKeys & Keys.Control) == Keys.Control && e.KeyCode == Keys.B)
{
//Code for the bold text
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold);
e.SuppressKeyPress = true;
}
else if ((Control.ModifierKeys & Keys.Control) == Keys.Control && e.KeyCode == Keys.I)
{
//Code for the Italic
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Italic);
e.SuppressKeyPress = true;
}
else if((Control.ModifierKeys & Keys.Control) == Keys.Control && e.KeyCode == Keys.U)
{
//Code for the Underline
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Underline);
e.SuppressKeyPress = true;
}
else if ((Control.ModifierKeys & Keys.Control) == Keys.Control && e.KeyCode == Keys.T)
{
//Code for the StrikeOut
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Strikeout);
e.SuppressKeyPress = true;
}
}
}
}