Click here to Skip to main content
15,897,226 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 83.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 WinFormsCodeBox
{
  public  class StringDifferenceRange
    {
      public enum EStringDifferenceType
      {
          none,
          insert,
          append,
          delete,
          update
      }

      private int mCharactersFromFront = 0;
      private int mCharactersFromEnd = 0;
      private EStringDifferenceType mStringDifferenceType;
      public StringDifferenceRange(string firstString, string secondString)
      {
          if (firstString == secondString)
          {
              mStringDifferenceType = EStringDifferenceType.none;
          }
          else if (secondString.StartsWith(firstString))
          {
              mStringDifferenceType = EStringDifferenceType.append;
          }
          else
          {
              char[] firstChars = firstString.ToCharArray();
              char[] secondChars = secondString.ToCharArray();
              char[] smallerChars = null;
              char[] largerChars = null;
              if (firstString.Length < secondString.Length)
              {
                  mStringDifferenceType = EStringDifferenceType.insert;
                  smallerChars = firstChars;
                  largerChars = secondChars;
              }
              else if (firstString.Length > secondString.Length)
              {
                  mStringDifferenceType = EStringDifferenceType.delete;
                  smallerChars = secondChars;
                  largerChars = firstChars;
              }
              else
              {
                  mStringDifferenceType = EStringDifferenceType.update;
                  smallerChars = firstChars;
                  largerChars = secondChars;
              }

              for (int i = 0; i < smallerChars.Length; i++)
              {
                  if (smallerChars[i] != largerChars[i])
                  {
                      mCharactersFromFront = i;
                  }

              }
              for (int i = 0; i < smallerChars.Length; i++)
              {
                  if (smallerChars[smallerChars.Length -1 - i] != largerChars[largerChars.Length -1  - i])
                  {
                      mCharactersFromEnd = i;
                  }

              }



          }


      }     


      public EStringDifferenceType StringDifferenceType 
      {
          get { return mStringDifferenceType; }

      }

      public int CharactersFromFront
      {
          get { return mCharactersFromFront; }

      }
      public int CharactersFromEnd
      {
          get { return mCharactersFromEnd; }

      }

    }
}

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