Hi Guys, In this tutorial we will learn how to make a TextEditor windows form app. So lets Start.

Open Visual Studio File > New > Create New Project

Select Windows Form App

Now in toolbox Search For "RichTextBox"

Now Paste the RIchTextBox in the Main Form

1. Button for Bold Text in RichText Editor:

Now in tool box search for Button and Past it in the Form

if you want to add Image in the Button then just right click on the Button -> Properties -> Image here you can upload icon for your button.

After uploading the icon your button will look like the below image:

Now double click on the button, it will generate the Click eveent function in the form.cs

        private void button1_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + 
            richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }

So this will make the Selected Text in the RichTextEditor Bold

2. Text Underline Button For RichText Editor:

Similary add one button in the form, add image to that button change the name of that button to underlineBtn.

Double click on the Button that will generate click event for this button in the form.cs

        private void underlineBtn_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Underline);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + 
            richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }

the above code will make the selected text underlined on the click event

3.StrikeOut button for TextEditor:

Similarly add one button in form, replace name of the button as strikeOverBtn, replace text with image.

Double click on the button to generate the click event.

        private void strikeOverBtn_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Strikeout);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + 
            richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }

this function will make the selected text strikeout.

4. Text Italic Button for RichText Editor:

Similarly add another button in form, rename it as italicBtn, give icon to that button.

        private void italicBtn_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Italic);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + 
            richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }

this function will make your selected text italic.

5. Text Regular button for Rich Text Editor:

Similarly add another button in form, rename it as regularBtn, give icon to the button, double click on that button to generate its click event in form.cs

        private void regularBtn_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Regular);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + 
            richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }

this function will make your selected text to regular one.

6. Text To Upper Case button for Rich Text Editor:

Similarly add another button in form, rename it as textCapitalBtn, give icon to the button, double click on that button to generate its click event in form.cs

        private void textCapitaliseBtn_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText=richTextBox1.SelectedText.ToUpper();
        }

this function will make selected text to upper case letters.

7. Text To Lower Case button for Text Editor:

Similarly add another button in form, rename it as textToLowerBtn, give icon to the button, double click on that button to generate its click event in form.cs

        private void textToLowerBtn_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = richTextBox1.SelectedText.ToUpper();
        }

this function will lower case the selected text.

8. Bullet Point for TextEditor:

Similarly add another button in form, rename it as button2_Click, give icon to the button, double click on that button to generate its click event in form.cs

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionBullet = true;
        }

this will generate bullet point in the Selection Start point.

9. Color the text in TextEditor:

Similarly add another button in form, rename it as colorPicker, double click on that button to generate its click event in form.cs

        private void colorPicker_Click(object sender, EventArgs e)
        {
            ColorDialog MyDialog = new ColorDialog();
            MyDialog.AllowFullOpen = false;
            MyDialog.ShowHelp = true;
            MyDialog.Color = richTextBox1.ForeColor;
            if (MyDialog.ShowDialog() == DialogResult.OK)
                richTextBox1.SelectionColor = MyDialog.Color;
        }

this wil change the color of the Selection Text

10. Highlight the text in RichTextEditor:

Similarly add another button in form, rename it as highLightText, double click on that button to generate its click event in form.cs

        private void highLightText_Click(object sender, EventArgs e)
        {
            ColorDialog MyDialog = new ColorDialog();
            MyDialog.AllowFullOpen = false;
            MyDialog.ShowHelp = true;
            MyDialog.Color = richTextBox1.ForeColor;
            if (MyDialog.ShowDialog() == DialogResult.OK)
                richTextBox1.SelectionBackColor = MyDialog.Color;
        }

This will Highlight your text with desired Color.

11. Changing the font size in RichTextBox:

Add one combobox, name it as fontSize, click on the to create the click event on form.cs

        private void fontSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, 
            Convert.ToInt32(comboBox1.Text), FontStyle.Regular);
        }

this will change the font size of the selected text.

12. Text Alignment for RichTextEditor:

Add three buttons named as alignLeft,alignCenter,alignRight, click on them to create there respective click event.

        private void alignLeft_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
        }

        private void alignCenter_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
        }

        private void alignRight_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
        }

13. Adding Image in RichTextEditor:

Similarly add another button in form, rename it as addImage, double click on that button to generate its click event in form.cs

        private void addImage_Click(object sender, EventArgs e)
        {
            FileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Images |*.bmp;*.jpg;*.png;*.gif;*.ico";
            openFileDialog1.FileName = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                Image img = Image.FromFile(openFileDialog1.FileName);
                Clipboard.SetImage(img);
                richTextBox1.Paste();
                richTextBox1.Focus();
            }
            else
            {
                richTextBox1.Focus();
            }
        }

This Function will help you to select your desired image and paste it in the RichTextEditor.

13. Adding Select All Button in RichTextBox:

Similarly add another button in form, rename it as selectAll, double click on that button to generate its click event in form.cs

        private void selectAll_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
            richTextBox1.Focus();
        }

this code will select all text present in the richTextBox.

14. Copy Selected Text in RichTextEditor:

Add another button in the form named it as copySelected, double on the button to create its click event on form.cs

        private void copySelected_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
            richTextBox1.Focus();
        }

this will copy your selected files to the clipboard.

15. Past Text in RichTextEditor:

Add another button in the form named it as pastSelected, double on the button to create its click event on form.cs

        private void copySelected_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
            richTextBox1.Focus();
        }

this will paste your coppied text to the richTextBox.

16.Search Text in RichTextBox:

Add one text box in the form intialize the KeyUp event in the Main Function of the form.

        public Form1()
        {
            InitializeComponent();

            textBox3.KeyUp += TextBox3_KeyUp;
        }

KeyUp Event will trigged when you write something in the text box then find the corresponding text in the richTextBox.

        private void TextBox3_KeyUp(object sender, KeyEventArgs e)
        {
            richTextBox1.Undo();
            int i = 0;
            i = richTextBox1.Find(textBox3.Text, i, RichTextBoxFinds.WholeWord);
            while (i != -1)
            {
                richTextBox1.Select(i, textBox3.Text.Length);
                richTextBox1.SelectionColor = Color.Red;
                i = richTextBox1.Find(textBox3.Text, i + textBox3.Text.Length, 
                RichTextBoxFinds.None);
            }
        }

 

Your Final Code will be:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RichTextEditor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            textBox3.KeyUp += TextBox3_KeyUp;
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }
        
        private void underlineBtn_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Underline);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }

        private void strikeOverBtn_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Strikeout);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }

        private void italicBtn_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Italic);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }

        private void regularBtn_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Regular);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }

        private void textCapitaliseBtn_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText=richTextBox1.SelectedText.ToUpper();
        }

        private void textToLowerBtn_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = richTextBox1.SelectedText.ToUpper();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionBullet = true;
        }

        private void colorPicker_Click(object sender, EventArgs e)
        {
            ColorDialog MyDialog = new ColorDialog();
            MyDialog.AllowFullOpen = false;
            MyDialog.ShowHelp = true;
            MyDialog.Color = richTextBox1.ForeColor;
            if (MyDialog.ShowDialog() == DialogResult.OK)
                richTextBox1.SelectionColor = MyDialog.Color;
        }

        private void highLightText_Click(object sender, EventArgs e)
        {
            ColorDialog MyDialog = new ColorDialog();
            MyDialog.AllowFullOpen = false;
            MyDialog.ShowHelp = true;
            MyDialog.Color = richTextBox1.ForeColor;
            if (MyDialog.ShowDialog() == DialogResult.OK)
                richTextBox1.SelectionBackColor = MyDialog.Color;
        }

        private void fontSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, Convert.ToInt32(fontSize.Text), FontStyle.Regular);
        }

        private void alignLeft_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
        }

        private void alignCenter_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
        }

        private void alignRight_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
        }

        private void addImage_Click(object sender, EventArgs e)
        {
            FileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Images |*.bmp;*.jpg;*.png;*.gif;*.ico";
            openFileDialog1.FileName = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                Image img = Image.FromFile(openFileDialog1.FileName);
                Clipboard.SetImage(img);
                richTextBox1.Paste();
                richTextBox1.Focus();
            }
            else
            {
                richTextBox1.Focus();
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
            richTextBox1.Focus();
        }

        private void copySelected_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
            richTextBox1.Focus();
        }

        private void pasteSeleted_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
            richTextBox1.Focus();
        }

        private void TextBox3_KeyUp(object sender, KeyEventArgs e)
        {
            richTextBox1.Undo();
            int i = 0;
            i = richTextBox1.Find(textBox3.Text, i, RichTextBoxFinds.WholeWord);
            while (i != -1)
            {
                richTextBox1.Select(i, textBox3.Text.Length);
                richTextBox1.SelectionColor = Color.Red;
                i = richTextBox1.Find(textBox3.Text, i + textBox3.Text.Length, RichTextBoxFinds.None);
            }
        }
    }
}