?
Solved

highlight words with colour in RichTextBox control

Posted on 2003-02-24
7
Medium Priority
?
439 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

801 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