Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / Windows Forms

Fast Colored TextBox for Syntax Highlighting

Rate me:
Please Sign up or sign in to vote.
4.97/5 (878 votes)
24 Oct 2014LGPL323 min read 7.1M   104.2K   1.3K  
Custom text editor with syntax highlighting
using System.Windows.Forms;
using FastColoredTextBoxNS;
using System.Drawing;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Tester
{
    public partial class AutoIndentSample : Form
    {
        public AutoIndentSample()
        {
            InitializeComponent();
            cbAutoIndentType.SelectedIndex = 0;
        }

        private void cbAutoIndentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cbAutoIndentType.SelectedIndex == 0)//built-in C# AutoIndent
            {
                fctb.Language = Language.CSharp;
                fctb.AutoIndentNeeded -= new EventHandler<AutoIndentEventArgs>(fctb_AutoIndentNeeded);
                fctb.Text = @"/// Please, type next text (without slashes):
/// int Foo()
/// {
/// int i=10;
/// label:
/// while(i!=j){
/// i--;
/// j++;
/// }
/// if(i==0)
/// return i;
/// else
/// return j;
/// }

";
                fctb.GoEnd();
                fctb.Focus();
            }

            if(cbAutoIndentType.SelectedIndex == 1)//custom AutoIndent
            {
                fctb.Language = Language.Custom;
                fctb.AutoIndentNeeded += new EventHandler<AutoIndentEventArgs>(fctb_AutoIndentNeeded);
                fctb.Text = @"/// Please, type next text (without slashes):
/// begin
/// i := 1;
/// if j=0 then
/// begin
/// i := 10;
/// end
/// else
/// i := 20;
/// end

";
                fctb.GoEnd();
                fctb.Focus();
            }
        }

        void fctb_AutoIndentNeeded(object sender, AutoIndentEventArgs e)
        {
            // if current line is "begin" then next
            // line shift to right
            if (e.LineText.Trim() == "begin")
            {
                e.ShiftNextLines = e.TabLength;
                return;
            }
            // if current line is "end" then current
            // and next line shift to left
            if (e.LineText.Trim() == "end")
            {
                e.Shift = -e.TabLength;
                e.ShiftNextLines = -e.TabLength;
                return;
            }
            // if previous line contains "then" or "else", 
            // and current line do not contain "begin"
            // then shift current line to right
            if (Regex.IsMatch(e.PrevLineText, @"\b(then|else)\b") &&
                !Regex.IsMatch(e.LineText, @"\bbegin\b"))
            {
                e.Shift = e.TabLength;
                return;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer Freelancer
Ukraine Ukraine
I am Pavеl Tоrgаshоv, and I live in Kyiv, Ukraine.
I've been developing software since 1998.
Main activities: processing of large volumes of data, statistics, computer vision and graphics.

Comments and Discussions