ChgColorRTF




Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 60
'open tag

RichTextBox1.SelLength = 0
RichTextBox1.SelColor = &H8000000F
previous = KeyAscii
Case 62
'close tag

RichTextBox1.SelLength = 0
RichTextBox1.SelText = ">"
RichTextBox1.SelColor = &H0&
previous = KeyAscii
KeyAscii = 0

Case 33
'comments

If previous = 60 Then
RichTextBox1.SelStart = RichTextBox1.SelStart - 1
RichTextBox1.SelLength = 1
RichTextBox1.SelText = ""
RichTextBox1.SelLength = 0
RichTextBox1.SelColor = &HC00000
RichTextBox1.SelText = "<!"
previous = KeyAscii
KeyAscii = 0
End If
End Select
End Sub
'_________________________________________________________


Automated:
Sub ChangeColours()
Dim posEnd As Integer
i = 0
For i = 0 To Len(RichTextBox1.Text)
RichTextBox1.SelStart = i
RichTextBox1.SelLength = 1
If RichTextBox1.SelText = "<" Then 'start tag
posStart = i
End If
If RichTextBox1.SelText = ">" Then 'end tag
posEnd = i
End If
If RichTextBox1.SelText = "!" Then 'comment
previousChar = "!"
End If
If posEnd <> 0 Then
RichTextBox1.SelStart = posStart
RichTextBox1.SelLength = posEnd - posStart + 1
If previousChar <> "!" Then 'if Not comment
RichTextBox1.SelColor = &H8000000F
Else:
RichTextBox1.SelColor = &HC00000
previousChar = " "
End If
RichTextBox1.SelStart = posStart + 1
RichTextBox1.SelLength = 0
RichTextBox1.SelColor = &H0&
PosEnd = 0
posStart = 0
End If
Next i
End Sub

You need To Declare these variable within your forms
declarations:

For the On the Fly Declare:
Dim previous as Integer

For the automated declare:
Dim posStart As Integer
Dim previousChar As String

Assumes:You need to have a RTF box with the default
name (richtextbox1) on your













( chgcolorrtf.html )- by Paolo Puglisi - Modifica del 17/12/2023