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!

Automatically highlight text in a rich text box

Status
Not open for further replies.

wcwolff

Technical User
Aug 3, 2004
13
US
I am writing a program which displays a series of incidents from a log file in date order into a rich text box. I want to highlight the incident header in a color based on the type of incident with the text of the incident in black. The format looks like this:

INCIDENT TYPE 01/01/2005 user name (This is the header)
Text of incident

INCIDENT TYPE 01/01/2005 user name
Text of incident

... and so on.


I am loading the data into an array as below to fill the RTB.

Line(0) = "Header"
Line(1) = "Text"
Line(2) = "Header"
......and so on?"

RichTextBox1.Lines = Line

How can I cause each header line to be highlight in say red and bold with the text in standard black. I have only been able to get the text in all one color or another. Thanks in advance for any suggestions.
W. C. Wolff
 
Well, after the text is in the clipboard you could iterate through each line of text search for keywords. Once found - highlight the entire line using SelectionStart, and SelectionLength. Then change the SelectionColor and SelectionFont (font for bold).

So code would look akin to:
Code:
Dim iPos As Integer = 0
For i as Integer = 0 to RichText.Lines.Count - 1
  If InStr(RichText.Lines(i),"MyText") Then
    RichText.SelectionStart = iPos
    RichText.SelectionLength = RichText.Lines(i).Length
    RichText.SelectionColor = Color.Red
    RichText.SelectionFont = new Font(RichText.Font,RichText.FontSize,FontStyle.Bold)
  End If
  iPos += RichText.Lines(i).Length
End For
That wasn't tested but something along those lines should work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top