HTML Syntax Highlighting


General Info

HTML syntax highlighting can be done easier than other languages because it follows a set pattern. You have tags, attributes, comments, and text. That's about it...So to make highlighting faster, I created a routine that works just for HTML. It's faster because it follows the set pattern for HTML instead of just finding keywords and highlighting those.


It works by first going and putting in the correct RTF color table switches and then storing all text up to an HTML tag (thus making it faster if you have more text b/c it skips everything in between). Then it highlights an individual tag at a time. It goes through and evaluates each tag and its constituients. It's pretty basic...anything to the left of an equal sign in the tag is going to be an attribute name (this color is the same as the tag color...I'll work on creating separate colors for this), and anything to the right will be the attribute value (colored in blue in the example). It will color values even if they're not enclosed in quotes. When you go to the next space, then we know we're at another attribute name EXCEPT in cases when we're in a quote. I had to add some code to make sure it didn't evaluate the next word after a space as an attribute name instead of as part of the quote. But that's all taken care of, so don't fret! (c: It will color comments as well.


Don't try using TagBold, TagItalics, etc. b/c in some configurations it will cause it to freeze and I don't know why yet. I will work on that. But it does work fine w/ any color configuration. You can use common dialogs to allow the user to select a color and it will work fine w/ those. It automattically interprets the colors and applies them to the RTF.


If you use the supplied owner created RichText box, you need to download the SSubTimer.DLL - great subclassing control/activex dll from vbAccelerator. The supplied project uses the standard RichTextBox control that comes with VB - if you want to change the DLL to use the supplied RTF control, you'll need to change the code slightly - see below.


It's rather simple, actually...I have it set up w/ an RTB made through code, but you can easily switch it to a normal RTB by changing the "Public RichTxtBox As HSRichEdit" to "Public RichTxtBox As RichTextBox". After that, to highlight, just use two lines of code:

  set syntaxHighlight1.RichTxtBox =  RichTextBox1

To get it to color properly while typing, use the following code:

  Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
    KeyAscii = syntaxHighlight1.KeyPressEvent(KeyAscii)
  End Sub

That's all it takes to set it up! Good luck! (c;

-David Hoyt

Download zipped project file (51k)


  • Syntax Highlighting

    Posted by Legacy on 03/27/2003 12:00am

    Originally posted by: Ramil

    It odesn't work when I download the sample.

  • Speed Question and inserting info

    Posted by Legacy on 01/07/2001 12:00am

    Originally posted by: Ackbar

    First off I modified this code a bit but I had a couple of thoughts on it. I noticed that as it got further into the process it got substnatially slower. An example being I ran a test on a 68k html file. What I found was that the first 50% took just over 1 second. But the entire file took 23 seconds to highlight. Any suggestions on what could be causing this? I considered the posibility that it's maybe instr or such not liking to be used on such a huge file. If this is the case my solution would be to have a second variable and strip off everything thats already been checked. IE You have
    < html > blah blah blah < /html >

    Well it would first find the < html > and highlight that. So why keep it? Cut that section off the string.

    The second issue is how do you highlight info thats being dropped in? IE if you have table dialog. After it's all setup how would you highlight that?

    Another thing i've been wondering about. I coded it in so it would keep syntax with whatever it is around. But I noticed a few flaws in overall. Like if you type ="blah in tags and then delete that you still tend to end up with the red. This is easy enough to fix.

    Also you should consider adding a script section. Like isScript boolean or something. The reason I suggest this is fairly simple. When you highlight say

    < script >
    if (a < b)

    everything after that < gets highlighted. But this is only meant to be used as a < than sign. So a script coloring would be great. Maybe just leave everything in the script black. Maybe even go as far as highlighting keywords in the script.

    Anyway overall I think this is a good example. And I'm screwing around with it to try and optimize it for speed and quality. If completed it could be a very nice thing for vb programmers.

    If you have any suggestions etc, or if you have a solution to the inserting text problem please send me an email. (

    Thanks :)


  • Color Coding inserted text

    Posted by Legacy on 11/26/1999 12:00am

    Originally posted by: Eric Banker

    I have been able to get this part working great within my program but html editors tend to insert text into the textbox. I can not figure out how to create a new function that will highlight just the string that i want to insert. Do you have any ideas?

    Please email me with an answer if you can


    Eric Banker

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Thanks to the Internet of Things (IoT), physical assets are turning into participants in real-time global digital markets. The countless types of assets around us will become as easily indexed, searched and traded as any online commodity. While some industries will be tougher to transform than others – those with physical limitations, such as manufacturing, will be harder to digitize – untold economic opportunities exist for growth and advancement. Our research shows this will create a new "Economy …

  • By providing developers with the right tools to detect, understand, and fix problems early, your business can simplify software development, shorten development lifecycles, and improve the quality of software code. The end result is increased innovation, secure applications, and a faster time to market — all at a lower cost.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date