Results 1 to 15 of 15

Thread: Color Coding Text

  1. #1

    Thread Starter
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Unhappy Color Coding Text

    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

  2. #2
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    i just helped GlenW out with the exact same request !

    see here: http://www.vbforums.com/showthread.p...hreadid=166148
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  3. #3
    Si_the_geek
    Guest
    I just had a quick fiddle with that code - I've made it more than 10 times faster!

    here you go:
    VB Code:
    1. Private Sub HighlightText(keywords, iColour As Long)
    2.  
    3. Dim checkstr1 As String, checkstr2 As String
    4. Dim tmp_text As String
    5. Dim i As Integer
    6.  
    7. checkstr1 = Chr(32) & Chr(13) & Chr(10) & Chr(9) & Chr(46)
    8. checkstr2 = Chr(32) & Chr(13) & Chr(10) & Chr(9) & Chr(40)
    9. tmp_text = LCase(frmMain.rtbFormat.Text)
    10.  
    11.     Dim nStart As Integer, sPrevChar As String, sNextChar As String
    12.                                                
    13. For i = LBound(keywords) To UBound(keywords)
    14. sKeyword = keywords(i)
    15. nStart = InStr(1, tmp_text, sKeyword)
    16.  
    17.     Do While nStart <> 0
    18.         If nStart > 1 Then
    19.             sPrevChar = Mid$(tmp_text, nStart - 1, 1)
    20.         Else
    21.             sPrevChar = " "
    22.         End If
    23.         DoEvents
    24.         If Len(tmp_text) >= nStart + Len(sKeyword) Then
    25.             sNextChar = Mid$(tmp_text, nStart + Len(sKeyword), 1)
    26.         Else
    27.             sNextChar = " "
    28.         End If
    29.  
    30.         If InStr(1, checkstr1, sPrevChar) > 0 _
    31.         And InStr(1, checkstr2, sNextChar) > 0 Then
    32.             With frmMain.rtbFormat
    33.                 .SelStart = nStart - 1
    34.                 .SelLength = Len(sKeyword)
    35.                 .SelColor = iColour
    36.                 .SelText = StrConv(sKeyword, vbProperCase)
    37.                 .SelStart = Len(tmp_text)
    38.                 .SelColor = iColour
    39.             End With
    40.         End If
    41.  
    42.         nStart = InStr(nStart + Len(sKeyword), tmp_text, sKeyword)
    43.     Loop
    44. Next i
    45.  
    46. End Sub
    47.  
    48. Public Sub FormatCode(ByRef tmpRTB As RichTextBox)
    49. Dim oldselstart As Long
    50. Dim oldsellen As Long
    51.    
    52.    frmMain.rtbFormat.TextRTF = tmpRTB.TextRTF
    53.    oldselstart = tmpRTB.SelStart
    54.    oldsellen = tmpRTB.SelLength
    55.     With frmMain.rtbFormat
    56.        .SelStart = 0
    57.        .SelLength = Len(.Text)
    58.        .SelColor = lngTextColor
    59.        .SelStart = Len(.Text)
    60.     End With
    61.    
    62.                                        
    63.     ' Add more custom words here
    64. dim keywords
    65. keywords = Array("if", "doevents", "then", "else", "end", "with", "select", "case", _
    66.                   "private", "public", "dim", "sub", "function", "do", "loop", "for", _
    67.                   "next", "each", "integer", "boolean", "long", "double", "variant", _
    68.                   "string", "single", "byte", "date", "format", "cdate", "cvar", _
    69.                   "cbyte", "cbool", "cint", "clng", "csng", "cdbl", "const", _
    70.                   "option base 1", "option base 0", "until", "while", _
    71.                   "option explicit", "byval", "byref", "declare", "global", _
    72.                   "true", "false", "mod", "as", "lib", "open", "close", "append", _
    73.                   "output", "input", "print", "write", "binary", "access", "xor", _
    74.                   "or", "and", "in", "like", "typeof", "object", "ubound", "lbound", _
    75.                   "to", "on", "error", "goto")
    76.  
    77.    HighlightText keywords, lngKeywordColor
    78.  
    79.  
    80.  
    81.    Call FindComments(lngCommentColor)
    82.    tmpRTB.TextRTF = frmMain.rtbFormat.TextRTF
    83.    tmpRTB.SelStart = oldselstart
    84.    tmpRTB.SelLength = oldsellen
    85.    tmpRTB.SelColor = lngTextColor
    86.  
    87. End Sub

  4. #4

    Thread Starter
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Thanks Si,

    That code is faster. I've made more modifications to it and it works pretty well. I still cannot do real time editing though, but it works for me.

    Thanks,

  5. #5
    Lively Member
    Join Date
    Jan 1999
    Location
    California
    Posts
    115
    Have you ever thought about using the CodeMax control?

    You'll need to register to download, but it's well worth it.

    http://groups.yahoo.com/group/codemax/
    Ryan French
    Niresoft Incorporated
    http://www.niresoft.com
    [email protected]

  6. #6
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    For real time color coding, you should consider setting up a timer and when the timer fires, it color codes a few words at a time.

    This is how my icon extractor is working on my tree view and it is pretty smooth sailing.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  7. #7
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Si:

    in your FormatCode sub you have a tmpRTB parameter that you send to it, but then you dont send it to the HighlightText sub, in that sub ut uses frmMain.rtbFormat. just a thought...

    nice one anyway...

    another way to make it real fast would be if you knew all of the rich-text "codes" used by a richtextbox, you could then get the code to format in a string and without using the richtextbox's properties, format it, then just paste it to the rtb. that would be lightning quick
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  8. #8
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Originally posted by Si_the_geek
    I just had a quick fiddle with that code - I've made it more than 10 times faster!
    yours doesnt check for quote's i.e. "string"

    anything in quotes should not be effected - i.e. should not turn blue, unless it is preceded by a ', in which case it turns into a comment.

    also a ' doesnt count if its between 2 quotation marks.

    these are what slow the procedure down, since you have to check char by char to do that (kinda).
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  9. #9
    gaffa
    Guest
    As Ryan suggested, get the CodeMax control (it used to be at www.winmain.com but I think that site is dead) - it's got the kind of features that makes you wonder why you even considered writing your own syntax colouring code.

    Seriously well worth downloading.

    - gaffa

  10. #10
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    i usually write stuff just out of curiousity
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  11. #11
    Si_the_geek
    Guest
    i agree totally Darre1, but it wasn't my code - I just had to have a fiddle to make it more efficient... can't stand loose code

    If I had a need for it myself i would have written it completely differently... (made it real time for an editing environment, allowed for quotes & comments etc). I was just wasting a bit of time to boost the speed a little!

  12. #12
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    i hear you, its just that you speeded it up by removing some of its functionality

    the reason its slow is because i've checked character by character where needed, and you have to do that to capture the quotes and embedded 's etc
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  13. #13
    Si_the_geek
    Guest
    Originally posted by darre1
    i hear you, its just that you speeded it up by removing some of its functionality

    the reason its slow is because i've checked character by character where needed, and you have to do that to capture the quotes and embedded 's etc
    Erm... no I haven't - check out the first post in this thread, that it what I modified, not yours (I couldn't actually get yours to run cos of dll versions).

    The functionality I provided is exactly the same as was there before. But as you said it is far from perfect. I just had to make it more efficient!

  14. #14
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    my apologies, i should read more carefully

    BTW: mines not perfect either
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  15. #15

    Thread Starter
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    yours doesnt check for quote's i.e. "string"
    Darre1,

    This was only the section of my code that searches for the keywords, i also have a routine that searches for comments and quotes. I have also made some modifications to this version to account for numbers, "_", and other special chars.

    For the real time color formatting, I only trigger the event when the user hits the enter key right now, but it will account for whenever the user changes lines and something has changed.

    in your FormatCode sub you have a tmpRTB parameter that you send to it, but then you dont send it to the HighlightText sub, in that sub ut uses frmMain.rtbFormat. just a thought...
    The tmprtb is the RTB to be formatted. IN the format code routine, all of the text is copied to frmMain.rtbFormat, then copied back to tmprtb after all the formatting has been completed.

    Thanks for all you help,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width