title: VB.NET: Syntax Highlight
body: <p>I started to learn VB.NET and I'm trying to do a syntax highlight. The problem occurs when i set the color of selected text. It changes the whole richtextbox's content.</p>
  
  <pre><code>Private Sub txtText_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbText.TextChanged
      Dim keywords As ArrayList
      Dim index As Integer
      Dim keyboardCursorPosition As Integer
  
      keywords = New ArrayList()
  
      keywords.Add(New Keyword("&lt;?php", Color.Red))
      keywords.Add(New Keyword("echo", Color.Blue))
      keywords.Add(New Keyword("?&gt;", Color.Red))
  
      keyboardCursorPosition = rtbText.SelectionStart
  
      For Each keyword As Keyword In keywords
          index = rtbText.Text.IndexOf(keyword.getKey())
  
          If index &lt;&gt; -1 Then
              rtbText.Select(index, keyword.getKey().Length)
              rtbText.SelectionColor = keyword.getColor()
  
              rtbText.DeselectAll()
              rtbText.SelectionStart = keyboardCursorPosition
          End If
  
      Next
  End Sub
  </code></pre>