Results 1 to 7 of 7

Thread: Syntax Highlighter

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Syntax Highlighter

    I was wondering if it was possible to have a richtextbox be used as a program that highlights syntaxs.

    For example:

    Completed Syntaxs:
    <b>Hello!</b>
    <i>Hello!</i>
    <u><b>Hello!</b></u>

    ^^^
    If it's completed then it does this color

    <b>Hello!
    <u>Hello!

    ^^^
    If not then it does another color

    <br>
    <hr>

    ^^^
    And finally tags that don't require a closing tag are set to another color.
    Could somebody please give me some ideas on how to get started with this... or a commented working example?

    I'm making a program for some friends who want to start to learn HTML (Myspace is their motivation, lol).

  2. #2
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: Syntax Highlighter

    Yes, I have used a RichTextBox in order to highlight code. Keep in mind that you will have to write code that actually examines the contents of the RTB and then actually applies the color to the appropriate text items...!

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Syntax Highlighter

    Uhhh, how exactly would i go about doing this? I only really know basically what needs to be done, but there's two ways i can think of doing it.

    The first step is to search the entire richtextbox for certain strings, such as the <br>, then either replacing that with a special code that changes the color, or by selecting the text (the <br> text) & changing the properties.

    But for the rest of what i've asked such as showing an incomplete tag, i really have no idea =(.

  4. #4
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: Syntax Highlighter

    ...no mystery, since you already see what needs to be done!

    Yes, you've got to use either INSTR on the RTB.TEXT, or use the intrinsic FIND method of the RTB to find the tag pairs you want. Then set the .SelStart and SelLength appropriately. Then set the .SelColor and/or SelBold and/or SelItalic (well, you get the idea) and then continue until you reach the end of the .TEXT itself.

    Note that you do not need to REPLACE the <br> or other tags unless this suits you. You can leave them as they are, if you wish. If you want to get rid of them, be aware that you may have a problem; using VB's REPLACE function affects the .TEXT and will, by itself, cause the previous color highlighting to be removed (a Catch 22, for sure).

    So, as you see, this requires a fair bit of work...

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Syntax Highlighter

    I run into a few problems when i try this though

    Here's pretty much where i'm up to:

    VB Code:
    1. Public Sub Highlight_Single_Tags(Textbox_ As RichTextBox)
    2.  
    3. Dim i As Long
    4.  
    5. With Textbox_
    6.  
    7. For i = 1 To Len(.Text)
    8.  
    9. If Mid(.Text, i, 4) = "<br>" Then
    10. .SelStart = i - 1
    11. .SelLength = 4
    12. .SelColor = vbBlue
    13. .SelLength = 0
    14. End If
    15.  
    16. If Mid(.Text, i, 3) = "<p>" Then
    17. .SelStart = i - 1
    18. .SelLength = 3
    19. .SelColor = vbRed
    20. .SelLength = 0
    21. End If
    22.  
    23. Next
    24.  
    25. End With
    26.  
    27. End Sub


    Now, the problem with this way is that it moves the cursor around. So once it checks for the tags it will move the cursor back to the start of the tag, this can be annoying. Not only that, it stops you from typing after that because i want it to update the colors after every keypress, so every time you press a key, the cursor will jump back to the start of the last tag it changed.

    I don't want it to do that, i just want it to replace the color of the text without moving the cursor around. I don't know how to do this =(. Especially since it will be looking for multiple tags that can show up a number of times.

    Also removing the colors of the affected text when it is no longer a tag will be hard.

    And this is just for stand alone tags... This is going to be difficult.

    Any ideas/examples?
    Last edited by Slyke; Dec 20th, 2006 at 11:33 AM.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Syntax Highlighter

    Any ideas?

  7. #7
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: Syntax Highlighter

    ...well, you probably want to limit the events which cause syntax highlighting to occur when the user presses an logical word seperator (such as SPACE or TAB or ENTER, etc).

    And even then you can capture the .SelStart before using it and then reset it after the syntax highlighting code completes...?

    And the FOR...NEXT loop you show below can be much improved by the use of another variable, such as Y.

    VB Code:
    1. Y = 1
    2. DO
    3. X = INSTR(Y,TEXT,"<br>")
    4. IF X = 0 THEN EXIT DO
    5. ...other code to perform highlighting...
    6. Y = X + 1
    7. LOOP

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