?
Solved

highlight words with colour in RichTextBox control

Posted on 2003-02-24
7
Medium Priority
?
445 Views
Last Modified: 2008-02-01
I have a VB application with RichTextBox control that I load with text from a text file. During runtime, I want to replace few words and highlight them with some colour. I need some way desperately.

Thanks in advance.
0
Comment
Question by:chinawal
  • 4
  • 2
7 Comments
 

Expert Comment

by:dustybryn
ID: 8007730
Use the selcolour property of the richtextbox to colour a selected area

'the words "is a" will be green
eg. with richtextbox1
       .text = "This is a test"
       .selstart =6
       .sellength =4    
       .selcolour = vbgreen
    end with

0
 

Expert Comment

by:dustybryn
ID: 8007755
correction sorry, spelt color wrong

with richtextbox1
      .text = "This is a test"
      .selstart =5
      .sellength =4    
      .selcolor = vbgreen
   end with
0
 

Expert Comment

by:dustybryn
ID: 8007768
You can also do it this way


with richtextbox1
     .text = "This is a test"
     .find("is a")
     .selcolor = vbgreen
end with

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:chinawal
ID: 8008027
Dustybryn, Thanks for quick reply. Here are my comments:
1) FIND will highlight only the one word.
2) selstart, sellength with selcolor works fine, if multiple words to be highlighted. I already knew this way.
Please see my requirement as follows:
Dim X,Y
X = "S"
Y="SET"
rtfTextBox1.Text= Replace(rtfTextBox1.Text, X, Y)

I want to make sure that all the replaced words i.e. "SET" are highlighted.
0
 
LVL 5

Author Comment

by:chinawal
ID: 8008114
Dustybryn, Thanks for quick reply. Here are my comments:
1) FIND will highlight only the one word.
2) selstart, sellength with selcolor works fine, if multiple words to be highlighted. I already knew this way.
Please see my requirement as follows:
Dim X,Y
X = "S"
Y="SET"
rtfTextBox1.Text= Replace(rtfTextBox1.Text, X, Y)

I want to make sure that all the replaced words i.e. "SET" are highlighted.
0
 

Accepted Solution

by:
dustybryn earned 150 total points
ID: 8009288
The example below shows how to do this assuming you have a richtextbox called richtextbox1 and a command button called command1



Option Explicit

Private Sub HighlightText(RTB As RichTextBox, strText As String)
Dim iLen As Integer, iPos As Integer
With RTB
    iLen = Len(.Text)
    iPos = 1
    Do While iPos <= iLen And InStr(iPos, .Text, strText, vbTextCompare) > 0
        .Find strText, iPos, iLen
        .SelColor = vbGreen
        iPos = (.SelStart) + (Len(.SelText)) + 1
    Loop
   
End With

End Sub

Private Sub Command1_Click()
Call HighlightText(RichTextBox1, "SET")
End Sub

Private Sub Form_Load()
RichTextBox1.Text = "Big set small set juicy set saggy set"
End Sub
0
 

Expert Comment

by:zakkis
ID: 11839633
Great Man
It really works

Thanx a lot
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question