Link to home
Start Free TrialLog in
Avatar of chinawal
chinawalFlag for United States of America

asked on

highlight words with colour in RichTextBox control

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.
Avatar of dustybryn
dustybryn

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

correction sorry, spelt color wrong

with richtextbox1
      .text = "This is a test"
      .selstart =5
      .sellength =4    
      .selcolor = vbgreen
   end with
You can also do it this way


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

Avatar of chinawal

ASKER

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.
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.
ASKER CERTIFIED SOLUTION
Avatar of dustybryn
dustybryn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great Man
It really works

Thanx a lot