Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing selected word color in a Richtextbox.

Status
Not open for further replies.

wcwolff

Technical User
Aug 3, 2004
13
US
I am writing a program which keeps notes on individual customer accounts. I am using a richtextbox which I have never work with before and have little knowledge of.

There are three types of notes kept(Problems, Memos and Quotes). Individual notes will be loaded from files in to a richtextbox sequentially from oldest to newest. The notes are availale to all users on a read-only basis and not editable in the RTB one logged.

I want to highlight all of the same note type headers in the same color with each type in a diffrent color. (eg Warning in Red, Memo in blue, Quote in green)

I have written code which loads the data and formats it as listed below. The code I listed for changing the color works, but it only works on the last instance of each heading type - not on all instances of each listed in the RTB.

I need to know how to modify this code to highlight all the same memo type headers to be the same color, not just one of each. I assume I have to loop through or set a different starting point but I'm not sure how to go about it.

Thanks in advance for any help!
wcwolff

Sample of RTB format:

PROBLEM ...date & time & by whom
message text...blah....blah...blah

MEMO ...date & time & by whom
message text...blah....blah...blah

QUOTATION ...date & time & by whom
message text...blah....blah...blah

PROBLEM ...date & time & by whom
message text...blah....blah...blah

Etc...


Code:

private sub....

*****
Code to load memo data here - newest to oldest
*****


Me.RTB.ForeColor = Color.Black ' sets all text to black

' highlight selected words

Me.RTB.SelectionStart = RTB.Find("warning")
Me.RTB.SelectionColor = Color.Red

Me.RTB.SelectionStart = RTB.Find("memo")
Me.RTB.SelectionColor = Color.Blue

Me.RTB.SelectionStart = RTB.Find("quote")
Me.RTB.SelectionColor = Color.Green

end sub
 
You need to loop through the text and color each instance as it is found. Here's a sub I whipped up to do this:

Private Sub ColorText(ByRef RTBToSearch As RichTextBox, ByVal TextToColor As String, ByVal Color As System.Drawing.Color)

Dim StrPos As Integer
Dim StrLen As Integer
Dim FindStr As String

FindStr = TextToColor

StrLen = Len(FindStr)

StrPos = RTBToSearch.Find(FindStr)

Do While StrPos >= 0
RTBToSearch.SelectionStart = StrPos
RTBToSearch.SelectionLength = StrLen
RTBToSearch.SelectionColor = Color
StrPos = RTBToSearch.Find(FindStr, StrPos + StrLen, RichTextBoxFinds.None)
Loop

End Sub

You would call it like this:

ColorText(Me.RTB, "warning", Color.Red)

ColorText(Me.RTB, "memo", Color.Blue)

ColorText(Me.RTB, "quote", Color.Green)

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Thanks for the fast response. I really appreciate the assistance. It works perfectly!

I also added the following line of code in the Do...Loop which makes the colored text selection bold as well.

RTB.SelectionFont = New Font("Microsoft Sans Serif", 8, FontStyle.Bold)


Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top