-
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
-
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
-
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
-
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...
-
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
-
[/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,
-
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
-
On http://www.codeguru.com/vb are some nice working examples of syntax colouring for various languages incl. VB
-
Cheers Crazy D, that was exactly what I was looking for ;)
Shaun
-
You can also use the CreateWindowEx API to create an instance of the class VbaWindow.