Results 1 to 20 of 20

Thread: HTML Highlighting

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    28

    HTML Highlighting

    I am currently working on a HTML Editor, and use a module to highlight the HTML code. This highlights the tags, attibute values, comments, etc in different colours, and works fine. The only problem is that it is VERY SLOW! With large files it can take up to 10 seconds to highlight, which is way to slow.

    As I'm quite new to VB, I'd appreciate it if you could help me with my problem by showing me code (or telling me where I can find it) which I could use. I don't really want to use any OCX files or anything like that. I would also like to make make comments italic, head/body/html tags bold etc.

    Please help,

    Tom
    Last edited by Tom_H; May 16th, 2001 at 12:24 PM.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    well I discovered this...
    at least for the visual effect

    I used 2 RichTextBoxes...1 hidden

    when Coloring was to occur (like when ENTER was pressed)
    It would copy text to the second window...do the color then give it back to the first...then it just flashes

    also look into getting the current line that is being typed on.
    so all you should try to color is the current line

    Download the code to my VBBrowser (link below) the code is a little hard to follow but what you need is in there...

    the code window in the browser will color each line after enter is hit. it is about 90-90% accurate
    a few bugs to work out still but...hey... no time
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Matthew Gates
    Guest
    Use this code, seems fast enough. Good for an HTML Editor.


    Code:
    'Author: Megatron
    'Origin: http://www.vbforums.com
    'Purpose: Highlight Text in RichTextBox Automatically
    'Version: VB5+ 
    
    
    
    
    Option Explicit
    Const vbDarkBlue = &H800000
    
    Private Sub HighlightText(sKeyword As String, iColour As Long)
        Dim nStart As Integer
        Dim sPrevChar As String
        Dim sNextChar As String
                                                
        nStart = InStr(1, LCase(RichTextBox1.Text), sKeyword)
                                                                                 
        Do While nStart <> 0
            If nStart > 1 Then
                sPrevChar = Mid$(RichTextBox1.Text, nStart - 1, 1)
            Else
                sPrevChar = " "
            End If
                                                        
            If Len(RichTextBox1.Text) >= nStart + Len(sKeyword) Then
                sNextChar = Mid$(RichTextBox1.Text, nStart + Len(sKeyword), 1)
            Else
                sNextChar = " "
            End If
                                                               
            If (sPrevChar = Chr(32) Or sPrevChar = Chr(13) Or _
            sPrevChar = Chr(10) Or sPrevChar = Chr(9)) And _
            (sNextChar = Chr(32) Or sNextChar = Chr(13) Or _
            sNextChar = Chr(10) Or sNextChar = Chr(9)) Then
            
            With RichTextBox1
                .SelStart = nStart - 1
                .SelLength = Len(sKeyword)
                .SelColor = iColour
                .SelText = StrConv(sKeyword, vbProperCase)
                .SelStart = Len(RichTextBox1.Text)
                .SelColor = vbBlack
            End With
        
            End If
                                                      
            nStart = InStr(nStart + Len(sKeyword), LCase(RichTextBox1.Text), sKeyword)
        Loop
    End Sub
    
    
    
    Private Sub Form_Resize()
        RichTextBox1.Move 0, 0, ScaleWidth, ScaleHeight
    End Sub
    
    Private Sub RichTextBox1_Change()
        With RichTextBox1
            .SelStart = 0
            .SelLength = Len(.Text)
            .SelColor = vbBlack
            .SelStart = Len(.Text)
        End With
        
                               
        ' Add more custom words here
        HighlightText "if", vbDarkBlue
        HighlightText "then", vbDarkBlue
        HighlightText "else", vbDarkBlue
        HighlightText "for", vbDarkBlue
        HighlightText "next", vbDarkBlue
        HighlightText "do", vbDarkBlue
        HighlightText "while", vbDarkBlue
        HighlightText "until", vbDarkBlue
        HighlightText "loop", vbDarkBlue
    End Sub

  4. #4
    Lively Member Peder's Avatar
    Join Date
    Apr 2001
    Posts
    102
    Maby the code is fast, but you have to write all the tags yourself, it had been much smarter if the code detect the < and > itself. Besides: The code doesn't highlight comment and values. I've never seen a good Highlighter to VB, they are all slow. It's weird that VB doesn't have it, because Delphi have plenty (and they are all VERY good).

    ---------------
    Peder

  5. #5
    Lively Member Peder's Avatar
    Join Date
    Apr 2001
    Posts
    102
    Maby the code is fast, but you have to write all the tags yourself, it had been much smarter if the code detect the < and > itself. Besides: The code doesn't highlight comment and values. I've never seen a good Highlighter to VB, they are all slow. It's weird that VB doesn't have it, because Delphi have plenty (and they are all VERY good).

    ---------------
    Peder

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    28
    I need it to be:

    - fast
    - highlight tags, as well as their values
    - highlight comments, css etc
    - make certain tags/comments italic or bold
    - update when the user hits return (or up or down keys)
    - no OCX's, DDL's etc

    Well the one I am currently using does most of the above, but is way too slow, and can take up to 10 secs to update when the user hits return (if working with large files).

    Anyone have a solution?

    Tom

  7. #7
    Tygur
    Guest
    I have writen some comparatively fast code in VB to highlight files. It takes 0.2 seconds to colorize (Is there a better word?) a 9K file (still too slow by my standards). I've attached an EXE that uses the code so you can test out its speed. You can use it to load a file, and it also colorizes as you type.

    Because so much trouble went into making the code, I'd rather not post it, but I will tell you what I did. It'll be very obvious why it was so much trouble.

    Basically, I looked at the RTF specification and wrote code to change the TextRTF property of the richtextbox so the html code can be colorized. This is much faster, but not nearly as easy.

    Is it fast enough for you guys? If so, I may make an OCX..

  8. #8
    Tygur
    Guest
    Oh, I didn't see your message for no OCX..
    If you want to try doing it yourself, I'll happily answer any questions you may have.. (Also, I'm considering changing my mind about the code, but I'm not sure)

  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    28
    ok, thanks, I'll take a look at it later.

    I may decide to turn to an ocx if i can't get a suitable solution as code, and as i am quite a beginner to vb (i've only been using it for about 1 or 2 months now), I may take you up on your offer for the ocx, as the method you suggested (looking at the RTF text spec) would probably be a bit too complex for me!

    Does it support cascading style sheet highlighting as well? And can you, for example, make all comment tags italic using it?

    Tom
    Last edited by Tom_H; May 17th, 2001 at 01:49 PM.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    28
    after looking at the exe you sent, i think that the one i am currently using is faster then your one...

    how can i write a bit of code to tell me how long my highlighter is taking to highlight in a MsgBox (like yours does in the title)? Then I can compare the two.

    if you want to c the code that i am currently using then just email me (it is too long to post here).

    Cheers,

    Tom

  11. #11
    Tygur
    Guest
    This is the way I did it in my sample:
    Code:
    Dim sngStart As Single, sngEnd As Single
    sngStart = Timer
    'Do the stuff you want to time here
    sngEnd = Timer
    Me.Caption = "Time Taken: " & Round(sngEnd - sngStart, 3) & " Seconds"

  12. #12

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    28
    I don't suppose anybody could tell me how I can highlight (or select) the line above the current line in a rich text box when the user presses return (if that makes sense!).

    Tom

  13. #13
    Addicted Member Dim A's Avatar
    Join Date
    Jul 2000
    Posts
    201
    This might help. This code will tell you the current cursor position after a user presses the return key.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    
    If KeyAscii = vbKeyReturn Then
        Debug.Print Text1.SelStart
    End If
    
    End Sub
    It should then just be a matter of counting back from this position to the last carriage return.

    - Dim A

  14. #14
    Addicted Member Dim A's Avatar
    Join Date
    Jul 2000
    Posts
    201

    More like this...

    You might like this code block better. I think there's some issue with vbcr/vblinefeed/vbnewline so you might want to look into that.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim x As Long
    Dim y As Long
    Dim found As Boolean
    Dim LastLine As String
    
    If KeyAscii = vbKeyReturn Then
        x = Text1.SelStart
        y = x
        
        While y > 1 And found = False
            y = y - 1
            If Mid$(Text1.Text, y, 1) = vbCr Then
                found = True
            End If
        Wend
    
        If y = 0 Then y = 1
    
        LastLine = Mid$(Text1.Text, y, x - y + 1)
        Debug.Print LastLine
    End If
    End Sub
    Anyways... y is the position of the CR just before the last line, x is the position after the last CR, and of course LastLine is the last line of text.

    Hope this helps, I didn't get a chance to polish it up.

    - Dim A
    Last edited by Dim A; May 17th, 2001 at 03:59 PM.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    28
    Thanks Dim A.

    Tyger, i sent u an email but it came back with "FAILURE NOTICE" !? is your email account working? i will try it again, and i'll send it to you using both of the addresses you gave me.

    Tom

  16. #16

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    28
    If anyone want to see the HTML editor, and also a CSS editor (which I only made today!), the EXE's can be downloaded from the links below. These files do not include any runtime files or OCX's (Rich Text Box, Windows Common Controls 6 SP-3, and Common Dialog).

    HTML Editor
    CSS Editor

    Tom

  17. #17

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    28
    If anybody has any suggestions for either of these two editors, please e-mail them to me, or just post them here.

    Cheers,

    Tom

  18. #18

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    28
    Ok guys, keep the suggestions coming...

    I have also updated the two files very slightly.

    HTML Editor (144kb)
    CSS Editor (18kb)

    These files contain no run-time files or OCX's.

    If anyone wants to see the code that I am using to highlight the html code, send me an e-mail.

    Tom

  19. #19
    Addicted Member
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    143

    Suggestion

    I suggest you use 2 different level of coding
    As in as the user types, you use seltext to go back and forward the letter the user types in and determine what colour to use. Then another level is using the slower colouring code that is much slower but for colouring a whole bunch of text at a go.
    If you don't get what i mean, try my Developer suite with autocomplete and color coding html editor from www.twk.2ya.com

    What i need now, is a even faster code to color the text.
    Unignal Software Team Developer
    -------------------------------------------
    Current Project:
    Noter Light
    -------------------------------------------
    http://www.unignal.sg.tf

  20. #20
    Addicted Member
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    143

    Btw

    Notice that in my program, when you paste some html codes inside, the code will be automatically coloured before insertion.
    Unignal Software Team Developer
    -------------------------------------------
    Current Project:
    Noter Light
    -------------------------------------------
    http://www.unignal.sg.tf

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