Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / Windows Forms

CodeBox for Windows Forms

Rate me:
Please Sign up or sign in to vote.
4.95/5 (29 votes)
12 Nov 2009CPOL11 min read 82.5K   2.4K   99  
A RichTextBox for Windows Forms that supports flexible highlighting and background coloring.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TextUtils
{
    /// <summary>
    /// A pair of integers refering to the starting position and lenght of a piece of text
    /// </summary>
    public class TextIndex : IComparable<TextIndex>
    {
        /// <summary>
        ///The integer position of the first character
        /// </summary>
        public int Start { get; set; }
        
        /// <summary>
        /// Number of characters in range
        /// </summary>
        public int Length { get; set; }

        /// <summary>
        /// Contructor for the TextIndex
        /// </summary>
        public TextIndex() { }

        /// <summary>
        /// Contructor for the TextIndex
        /// </summary>
        public TextIndex(int start, int length)
        {
            Start = start;
            Length = length;
        }

        /// <summary>
        /// The integer position of the last character
        /// </summary>
        public int End
        {
            get { return Start + Length; }

        }

        public static TextIndex FromStartEnd(int start, int end)
        {

            return new TextIndex(start, end - start);

        }

        /// <summary>
        /// Returns the substring defined by the start and length of the textindex
        /// </summary>
        /// <param name="str">The string to extract the substring from</param>
        /// <returns>The Substring</returns>
        public string Text(string str)
        {
            return str.Substring(Start, Length);
        }


        public override string ToString()
        {
            return this.Start + "," + this.Length ;
        }


        public bool Contains(int position)
        {
            return (position >= Start && position <= End);

        }

        public int CompareTo(int position)
        {
            if (position < Start)
            {
                return  1;

            }
            else if (position > End)
            {

                return - 1;
            }
            else
            {
                return 0;
            }


        }

        public static TextIndex Parse(string text)
        {
            string[] strSplit = text.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
            if (strSplit.Length != 2)
            {
                throw new Exception("Bad format");
            }
            else
            {
               return new TextIndex(Int32.Parse(strSplit[0]), Int32.Parse(strSplit[1]) );
            }

        }

        #region IComparable<TextIndex> Members

        public int CompareTo(TextIndex other)
        {
            if ( Math.Sign( this.Start - other.Start) == 0) 
            {
                return Math.Sign(this.Length  - other.Length );
            }
            else
            {
                return Math.Sign(this.Start - other.Start);
            }
        }

        #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)
United States United States
Written software for what seems like forever. I'm currenly infatuated with WPF. Hopefully my affections are returned.

Comments and Discussions