PDA

Click to See Complete Forum and Search --> : Change text colors


rbnwares
Feb 9th, 2001, 11:43 PM
Is there any short way of changing a text contained in the rich textbox? I do want to create an editor that behaves like VB IDE and other great editors. For example, I want to change the color of the comment text to green, ordinary text to blue and keywords to blue.

Lord Orwell
Feb 10th, 2001, 02:01 AM
no.
here's how i did it for existing text:
set the text selection boundaries to include the text to modify.
then do a richtextbox.selcolor=newcolor
and unset the selection (or put it back to original)

For new text being parsed in, simply change the Richtextbox.selcolor as you go.

By the way, i found a bug in the richtextbox control.
when you have just the cursor on screen, and no text
selected and try to read the data of the text next to the cursor, it returns the values for the letter to the LEFT of the cursor, not the right. Quite annoying. I had a custom toolbar that updated pressed buttons depending on the value of the character next to the cursor (italic button, bold button, etc), and this really messed up how my program worked.

Feb 10th, 2001, 10:25 AM
Add to a Form with a RichTextBox.

Option Explicit
Const vbDarkBlue = &H800000
Const vbDarkGreen = &H8000&


Private Sub HighlightText(sKeyword As String, iColour As Long)

Dim nStart As Integer, sPrevChar As String, 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 = vbDarkBlue
.SelText = StrConv(.SelText, vbProperCase)
.SelStart = Len(RichTextBox1.Text)
.SelColor = iColour
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", vbBlue
HighlightText "then", vbBlue
HighlightText "end", vbBlue
HighlightText "else", vbBlue
HighlightText "open", vbBlue
End Sub

rbnwares
Feb 13th, 2001, 02:50 AM
Wow! Thanks a lot for ur help. I will try it as soon as possible. :) :)