Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

CodeTextBox - another RichTextBox control with syntax highlightning and intellisense

Rate me:
Please Sign up or sign in to vote.
4.87/5 (34 votes)
8 Sep 2008CPOL2 min read 145.3K   6.4K   147  
A RichTextBox control with syntax highlightning and intellisense.
using System;
using System.Windows.Forms;

namespace Moonlight
{
    internal class RichTextboxHelper
    {
        #region Methods
        public static string GetLastWord(RichTextBox richTextbox)
        {
            int pos = richTextbox.SelectionStart;

            while (pos > 1)
            {
                string substr = richTextbox.Text.Substring(pos - 1, 1);

                if (Char.IsWhiteSpace(substr, 0))
                {
                    return richTextbox.Text.Substring(pos, richTextbox.SelectionStart - pos);
                }

                pos--;
            }

            return richTextbox.Text.Substring(0, richTextbox.SelectionStart);
        }
        public static string GetLastLine(RichTextBox richTextbox)
        {
            int charIndex = richTextbox.SelectionStart;
            int currentLineNumber = richTextbox.GetLineFromCharIndex(charIndex);

            // the carriage return hasn't happened yet... 
            //      so the 'previous' line is the current one.
            string previousLineText;
            if (richTextbox.Lines.Length <= currentLineNumber)
                previousLineText = richTextbox.Lines[richTextbox.Lines.Length - 1];
            else
                previousLineText = richTextbox.Lines[currentLineNumber];

            return previousLineText;
        }       
        public static string GetCurrentLine(RichTextBox richTextbox)
        {
            int charIndex = richTextbox.SelectionStart;
            int currentLineNumber = richTextbox.GetLineFromCharIndex(charIndex);

            if (currentLineNumber < richTextbox.Lines.Length)
            {
                return richTextbox.Lines[currentLineNumber];
            }
            else
            {
                return string.Empty;
            }
        }
        public static int GetCurrentLineStartIndex(RichTextBox richTextbox)
        {
            return richTextbox.GetFirstCharIndexOfCurrentLine();
        }
        #endregion
    }
}

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
Software Developer (Senior) Moonlight Studios
Hungary Hungary
I'm working as a software developer since 2005 mostly using C# and MS SQL Server for building windows and web applications. I have 3 years experience in developing CRM softwares, but I'm also interested in Game development using C++, DirectX, and C#-XNA.
I'm always open for new technologies, preferring code reusability, stability, and "nice solutions".

Comments and Discussions