title: RichTextBox syntax highlighting
body: <p>I am working with <a href="http://www.codeproject.com/Articles/10675/Enabling-syntax-highlighting-in-a-RichTextBox" rel="noreferrer">this code</a>. It is for syntax highlighting in a <code>RichTextBox</code>. I am specifically looking at the function <code>ProcessLine()</code> and <code>OnTextChanged()</code>, which I have modified as such:</p>
  
  <pre><code>protected override void OnTextChanged(EventArgs e)
  {
      // Calculate stuff here.
      m_nContentLength = this.TextLength;
  
      int nCurrentSelectionStart = SelectionStart;
      int nCurrentSelectionLength = SelectionLength;
  
      m_bPaint = false;
  
      // Find the start of the current line.
      m_nLineStart = nCurrentSelectionStart;
      while ((m_nLineStart &gt; 0) &amp;&amp; (Text[m_nLineStart - 1] != '\n'))
          m_nLineStart--;
      // Find the end of the current line.
      m_nLineEnd = nCurrentSelectionStart;
      while ((m_nLineEnd &lt; Text.Length) &amp;&amp; (Text[m_nLineEnd] != '\n'))
          m_nLineEnd++;
      // Calculate the length of the line.
      m_nLineLength = m_nLineEnd - m_nLineStart;
      // Get the current line.
      m_strLine = Text.Substring(m_nLineStart, m_nLineLength);
  
      // Process this line.
      ProcessLine();
  
      m_bPaint = true;
  }
  
  // Process a line.
  private void ProcessLine()
  {
      // Save the position and make the whole line black
      int nPosition = SelectionStart;
      SelectionStart = m_nLineStart;
      SelectionLength = m_nLineLength;
      SelectionColor = Color.Black;
  
      /*// Process the keywords
      ProcessRegex(m_strKeywords, Settings.KeywordColor);
      // Process numbers
      if(Settings.EnableIntegers)
          ProcessRegex("\\b(?:[0-9]*\\.)?[0-9]+\\b", Settings.IntegerColor);
      // Process strings
      if(Settings.EnableStrings)
          ProcessRegex("\"[^\"\\\\\\r\\n]*(?:\\\\.[^\"\\\\\\r\\n]*)*\"", Settings.StringColor);
      // Process comments
      if(Settings.EnableComments &amp;&amp; !string.IsNullOrEmpty(Settings.Comment))
          ProcessRegex(Settings.Comment + ".*$", Settings.CommentColor);*/
  
      SelectionStart = nPosition;
      SelectionLength = 0;
      SelectionColor = Color.Red;
  
      m_nCurSelection = nPosition;
  }
  </code></pre>
  
  <ul>
  <li><p>My first question is, when I enter into the <code>ProcessLine()</code> in <code>OnTextChanged()</code>, will I always have a newline character at the end of <code>m_strLine</code>? Will the smallest value or <code>m_strLine</code> be "\n" and the largest "any#ofchars+\n"?</p></li>
  <li><p>And just so I have this right, <code>SelectionStart</code> is my caret position if <code>SelectionLength</code> is zero, and if <code>SelectionLength</code> is greater than zero my caret is at <code>SelectStart+SelectionLength</code>?</p></li>
  <li><p>I am trying to modify this code to color a whole lot of different syntax expressions, and I plan to go through it one character at a time, for each line. How might this fair when pasting or loading a file of 20k+ lines?</p></li>
  </ul>