Does anyone have a very EFFICIENT way of color coding text. I am using some code I found from Megatron, but its too slow. This is what I am using right now. If there is a large code segment it takes a few seconds to format the code.
VB Code:
  1. 'From Megatron
  2. Private Sub HighlightText(sKeyword As String, iColour As Long)
  3.     Dim nStart As Integer, sPrevChar As String, sNextChar As String
  4.                                                
  5.     nStart = InStr(1, LCase(frmMain.rtbFormat.Text), sKeyword)
  6.  
  7.     Do While nStart <> 0
  8.         If nStart > 1 Then
  9.             sPrevChar = Mid$(frmMain.rtbFormat.Text, nStart - 1, 1)
  10.         Else
  11.             sPrevChar = " "
  12.         End If
  13.          DoEvents
  14.         If Len(frmMain.rtbFormat.Text) >= nStart + Len(sKeyword) Then
  15.             sNextChar = Mid$(frmMain.rtbFormat.Text, nStart + Len(sKeyword), 1)
  16.         Else
  17.             sNextChar = " "
  18.         End If
  19.                                                            
  20.         If (sPrevChar = Chr(32) Or sPrevChar = Chr(13) Or _
  21.         sPrevChar = Chr(10) Or sPrevChar = Chr(9) Or sPrevChart = Chr(46)) And _
  22.         (sNextChar = Chr(32) Or sNextChar = Chr(13) Or _
  23.         sNextChar = Chr(10) Or sNextChar = Chr(9) Or sNextChar = Chr(40)) Then
  24.             With frmMain.rtbFormat
  25.                 .SelStart = nStart - 1
  26.                 .SelLength = Len(sKeyword)
  27.                 .SelColor = iColour
  28.                 .SelText = StrConv(sKeyword, vbProperCase)
  29.                 .SelStart = Len(frmMain.rtbFormat.Text)
  30.                 .SelColor = iColour
  31.             End With
  32.         End If
  33.                                                  
  34.         nStart = InStr(nStart + Len(sKeyword), LCase(frmMain.rtbFormat.Text), sKeyword)
  35.     Loop
  36. End Sub
  37.  
  38.  
  39. Public Sub FormatCode(ByRef tmpRTB As RichTextBox)
  40. Dim oldselstart As Long
  41. Dim oldsellen As Long
  42.    
  43.    frmMain.rtbFormat.TextRTF = tmpRTB.TextRTF
  44.    oldselstart = tmpRTB.SelStart
  45.    oldsellen = tmpRTB.SelLength
  46.     With frmMain.rtbFormat
  47.        .SelStart = 0
  48.        .SelLength = Len(.Text)
  49.        .SelColor = lngTextColor
  50.        .SelStart = Len(.Text)
  51.     End With
  52.    
  53.                                        
  54.     ' Add more custom words here
  55.    
  56. HighlightText "if", lngKeywordColor
  57. HighlightText "doevents", lngKeywordColor
  58. HighlightText "then", lngKeywordColor
  59. HighlightText "else", lngKeywordColor
  60. HighlightText "end", lngKeywordColor
  61. HighlightText "with", lngKeywordColor
  62. HighlightText "select", lngKeywordColor
  63. HighlightText "case", lngKeywordColor
  64. HighlightText "private", lngKeywordColor
  65. HighlightText "public", lngKeywordColor
  66. HighlightText "dim", lngKeywordColor
  67. HighlightText "sub", lngKeywordColor
  68. HighlightText "function", lngKeywordColor
  69. HighlightText "do", lngKeywordColor
  70. HighlightText "loop", lngKeywordColor
  71. HighlightText "for", lngKeywordColor
  72. HighlightText "next", lngKeywordColor
  73. HighlightText "each", lngKeywordColor
  74. HighlightText "integer", lngKeywordColor
  75. HighlightText "boolean", lngKeywordColor
  76. HighlightText "long", lngKeywordColor
  77. HighlightText "double", lngKeywordColor
  78. HighlightText "variant", lngKeywordColor
  79. HighlightText "string", lngKeywordColor
  80. HighlightText "single", lngKeywordColor
  81. HighlightText "byte", lngKeywordColor
  82. HighlightText "date", lngKeywordColor
  83. HighlightText "format", lngKeywordColor
  84. HighlightText "cdate", lngKeywordColor
  85. HighlightText "cvar", lngKeywordColor
  86. HighlightText "cbyte", lngKeywordColor
  87. HighlightText "cbool", lngKeywordColor
  88. HighlightText "cint", lngKeywordColor
  89. HighlightText "clng", lngKeywordColor
  90. HighlightText "csng", lngKeywordColor
  91. HighlightText "cdbl", lngKeywordColor
  92. HighlightText "const", lngKeywordColor
  93. HighlightText "option base 1", lngKeywordColor
  94. HighlightText "option base 0", lngKeywordColor
  95. HighlightText "until", lngKeywordColor
  96. HighlightText "while", lngKeywordColor
  97. HighlightText "option explicit", lngKeywordColor
  98. HighlightText "byval", lngKeywordColor
  99. HighlightText "byref", lngKeywordColor
  100. HighlightText "declare", lngKeywordColor
  101. HighlightText "global", lngKeywordColor
  102. HighlightText "true", lngKeywordColor
  103. HighlightText "false", lngKeywordColor
  104. HighlightText "mod", lngKeywordColor
  105. HighlightText "as", lngKeywordColor
  106. HighlightText "lib", lngKeywordColor
  107. HighlightText "open", lngKeywordColor
  108. HighlightText "close", lngKeywordColor
  109. HighlightText "append", lngKeywordColor
  110. HighlightText "output", lngKeywordColor
  111. HighlightText "input", lngKeywordColor
  112. HighlightText "print", lngKeywordColor
  113. HighlightText "write", lngKeywordColor
  114. HighlightText "binary", lngKeywordColor
  115. HighlightText "access", lngKeywordColor
  116. HighlightText "xor", lngKeywordColor
  117. HighlightText "or", lngKeywordColor
  118. HighlightText "and", lngKeywordColor
  119. HighlightText "in", lngKeywordColor
  120. HighlightText "like", lngKeywordColor
  121. HighlightText "typeof", lngKeywordColor
  122. HighlightText "object", lngKeywordColor
  123. HighlightText "ubound", lngKeywordColor
  124. HighlightText "lbound", lngKeywordColor
  125. HighlightText "to", lngKeywordColor
  126. HighlightText "on", lngKeywordColor
  127. HighlightText "error", lngKeywordColor
  128. HighlightText "goto", lngKeywordColor
  129.  
  130.  
  131.  
  132.    Call FindComments(lngCommentColor)
  133.    tmpRTB.TextRTF = frmMain.rtbFormat.TextRTF
  134.    tmpRTB.SelStart = oldselstart
  135.    tmpRTB.SelLength = oldsellen
  136.    tmpRTB.SelColor = lngTextColor
  137.  
  138. End Sub

Any help would be great.

Thanks in advance