Results 1 to 10 of 10

Thread: VB - HTML Tag Coloring(Both Realtime and non realtime)

Threaded View

  1. #1

    Thread Starter
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    VB - HTML Tag Coloring(Both Realtime and non realtime)

    I was playing around with my own lil' notepad editor, and I needed some syntax coloring, so I made this little baby:

    1. Put a RichTextBox control on your form, call it "RTF1"

    2. Add a module to your project, and insert the following function:
    VB Code:
    1. Public Function ColorHTML(RTF As RichTextBox)
    2.  
    3. 'This is NOT realtime coloring - Run after file load etc.
    4. Dim iTagEnd, iTagStart, iTagLength  As Integer
    5. Dim sTag As String
    6.  
    7. 'We Need it to go fast, so **** visibility
    8. RTF.Visible = False
    9.  
    10. For i = 1 To Len(RTF.Text)
    11. If Not Mid(RTF.Text, i, 2) = "<%" Then 'ASP Tag, ignore it
    12.  If Mid(RTF.Text, i, 1) = "<" Then
    13.    If InStr(i, RTF.Text, ">") > 0 Then
    14.    iTagStart = InStr(i, RTF.Text, "<")
    15.    iTagEnd = InStr(i, RTF.Text, ">")
    16.    iTagLength = iTagEnd - iTagStart
    17.    RTF.SelStart = iTagStart
    18.    RTF.SelLength = iTagLength - 1
    19.    sTag = RTF.SelText
    20.    RTF.SelText = ""
    21.    RTF.SelColor = &H800000
    22.    RTF.SelText = sTag
    23.    RTF.SelColor = vbBlack
    24.   End If
    25.  End If
    26. End If
    27. Next
    28. 'We're not coloring anymore - so it's cool if we can see it ;)
    29. RTF.Visible = True
    30. RTF.SelColor = vbBlack
    31. End Function

    3. Now make a command button on your form, and in it's Click event put:
    VB Code:
    1. ColorHTML RTF1

    4. Run your program and click the command button, everyting inside "< >" will be colored dark blue.

    I know this isn't the fastest function - you can almost certainly optimize it, but it works, and it's a good place to start from.

    Cheers!
    Last edited by vbNeo; Nov 3rd, 2003 at 01:01 PM.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

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