Click here to Skip to main content
15,892,839 members
Articles / Desktop Programming / Windows Forms

Easily Create Your Own Parser

Rate me:
Please Sign up or sign in to vote.
4.81/5 (56 votes)
9 Jul 2011CPOL7 min read 184.2K   9.5K   160  
Create a hand made parser in VB.NET or C# easily and fast
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;

namespace TokenIcer
{
    public class ColorRichTextBox : System.Windows.Forms.RichTextBox
    {
        private static bool m_bPaint = true;
        private int m_nContentLength = 0;
        private string m_strLine = "";
        private int m_nLineLength = 0;
        private int m_nLineStart = 0;
        private int m_nLineEnd = 0;
        private int m_nCurSelection = 0;

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            if (m.Msg == 0x00f)
            {
                if (m_bPaint)
                    base.WndProc(ref m);
                else
                    m.Result = IntPtr.Zero;
            }
            else
                base.WndProc(ref m);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            m_nContentLength = this.TextLength;

            int nCurrentSelectionStart = SelectionStart;
            int nCurrentSelectionLength = SelectionLength;

            m_bPaint = false;

            m_nLineStart = nCurrentSelectionStart;
            while ((m_nLineStart > 0) && (Text[m_nLineStart - 1] != '\n'))
                m_nLineStart--;

            m_nLineEnd = nCurrentSelectionStart;
            while ((m_nLineEnd < Text.Length) && (Text[m_nLineEnd] != '\n'))
                m_nLineEnd++;

            m_nLineLength = m_nLineEnd - m_nLineStart;
            m_strLine = Text.Substring(m_nLineStart, m_nLineLength);

            //ProcessLine();

            m_bPaint = true;
            //base.OnTextChanged(e);
        }

        private void ProcessLine()
        {
            int nPosition = SelectionStart;
            SelectionStart = m_nLineStart;
            SelectionLength = m_nLineLength;
            SelectionColor = Color.Black;

            Regex r = new Regex("[0-9]+");
            Match m = r.Match(m_strLine);

            if (m.Success && m.Length > 0)
            {
                int nStart = m_nLineStart + m.Index;
                int nLength = m.Length;
                SelectionStart = nStart;
                SelectionLength = nLength;
                SelectionColor = System.Drawing.Color.Red;
            }



            SelectionStart = nPosition;
            SelectionLength = 0;
            SelectionColor = Color.Black;
            m_nCurSelection = nPosition;
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Web Developer http://www.icemanind.com
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions