Windows Forms: Multiple Color In RichTextBox in C#

By FoxLearn 5/24/2017 9:08:09 PM   4.83K
How to Use Multiple Color in RichTextBox using C#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "MultipleColorRichText" and then click OK

multiple color richtextStep 3: Design your form as below

multiple color richtext

Add a ColorDialog to your form as below

color dialog in c#

Step 3: Add code to handle your form

using System;
using System.Drawing;
using System.Windows.Forms;

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

        Color _color = Color.Blue;
        bool _first = true;

        private void btnSend_Click(object sender, EventArgs e)
        {
            //Set color to RichTextBox
            rtfResult.SelectionColor = _color;
            if (_first)
                rtfResult.SelectedText = txtValue.Text;
            else
                rtfResult.SelectedText = Environment.NewLine + txtValue.Text;
            _first = false;
            txtValue.Clear();
            txtValue.Focus();
        }

        private void pColor_Click(object sender, EventArgs e)
        {
            //Open color dialog
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                _color = colorDialog1.Color;
                pColor.BackColor = _color;
            }
        }
    }
}

VIDEO TUTORIALS