Results 1 to 10 of 10

Thread: Syntax Highlighting

  1. #1

    Thread Starter
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243

    Question

    Hi,
    How do I highlight code like VB in a Rich Text Box in the same way that this forum does (with ) and like it does in their developers code book. Is there a reference or component I can use or would I have to check the text myself through code and highlight it that way?

    Thanks

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  2. #2
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Not sure if this is what you mean, but try it anyway:

    Code:
    Private Sub Command1_Click()
     With RichTextBox1
          .SelStart = 10 ' Put any number you want here
          .SelLength = 14' and here
          .SetFocus
     End With
    End Sub

  3. #3

    Thread Starter
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi coox,
    Sorry, that isnt what I mean. What I am trying to do is to have an example of vb code in a RTB and then highight it like it does in VB. ie. Private Sub is in blue, Comments are in Green etc... I could check through the text myself changing the colours of certain words but I was wondering if there was some sort of component that would do this for me.

    Cheers anyway

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  4. #4
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Hmmm, dunno - the guys here at VB-World have figured something out, coz when you put "[code]" in your post the result get's nicely coloured. Really don't know though...

  5. #5
    Guest
    Is this what you mean? Add a RichTextBox and insert the following code into it.
    Code:
    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

  6. #6
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    [/code]
    I'm pretty sure they use an ActiveX DLL. Just ask John (after he's finished with Mrs. Le Pont) for it in Forum Feedback,
    Courgettes.

  7. #7

    Thread Starter
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi Megatron,
    Yes, that is what I am looking for only without the need to type in all the names of keywords etc.. Is there a reference or component I can use or will I have to check for them all manually (like the above code does).

    Cheers Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  8. #8
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386
    On http://www.codeguru.com/vb are some nice working examples of syntax colouring for various languages incl. VB
    Hope this helps

    Crazy D

  9. #9

    Thread Starter
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Cheers Crazy D, that was exactly what I was looking for

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  10. #10
    Guest
    You can also use the CreateWindowEx API to create an instance of the class VbaWindow.

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