VB icon

Change Colour of Specified Text (Like HTML tags) in a RTF Box

Email
Submitted on: 1/7/2015 4:59:00 AM
By: Eriond (from psc cd)  
Level: Advanced
User Rating: By 4 Users
Compatibility: VB 5.0, VB 6.0
Views: 471
 
     This sifts through all the text in a RTF file and changes the colour of html tags. It can be used ewither automatically or on the fly (as you type into the RTF box). It currently Chnages the colour of html tags (defined as anything between "<" and ">") and html comments (""). Enjoy! Please mail me with suggestions
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Change Colour of Specified Text (Like HTML tags) in a RTF Box
'**************************************
You need to declare these variable within your forms declarations: 
For the On the Fly Declare:
Dim previous as integer
For the automated declare:
posStart As Integer 
Dim previousChar As String
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Change Colour of Specified Text (Like HTML tags) in a RTF Box
' Description:This sifts through all the text in a RTF file and changes the colour of html tags. It can be used ewither automatically or on the fly (as you type into the RTF box). It currently Chnages the colour of html tags (defined as anything between "<" and ">") and html comments ("<!" to ">"). Enjoy! Please mail me with suggestions
' By: Eriond (from psc cd)
'
' Assumes:You need to have a RTF box with the default name (richtextbox1) on your form.
'
' Side Effects:No side effects have been noticed.
'**************************************

On The Fly:
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


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.