Results 1 to 2 of 2

Thread: Search & Highlight terms in a RTF Box

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    1

    Search & Highlight terms in a RTF Box

    Hi,

    I’m fairly new at VB.Net and I’m having a few problems highlighting text in a Rich Text Box. I am working on a text editor that will have some text in the RTF Box and a regular text box where the user can enter a search term. When the find button is clicked, the first occurrence of the search term in the RTF box is highlighted if it exists in the Rich TextBox. If find is clicked again, the highlight moves to the next occurrence of the search term and so on.

    What I have done so far (not sure if this is the best way) is to put the contents of the RTF box into a string variable, search the string with the RTF contents for the find term, highlight the term, and then remove the first occurrence of the search term and everything before it from the string. Problem is, the RTF textbox displayed will need to keep all of the text, so when I search again, the highlighting messes up.

    Can someone help me with this?

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    look at SelectedText propery of RTF
    VB Code:
    1. Private Sub WriteTextToRichTextBox()
    2.    ' Clear all text from the RichTextBox;
    3.    richTextBox1.Clear()
    4.    ' Set the font for the opening text to a larger Arial font;
    5.    richTextBox1.SelectionFont = New Font("Arial", 16)
    6.    ' Assign the introduction text to the RichTextBox control.
    7.    RichTextBox1.SelectedText = "The following is a list of bulleted items:" + ControlChars.Cr
    8.    ' Set the Font for the first item to a smaller size Arial font.
    9.    richTextBox1.SelectionFont = New Font("Arial", 12)
    10.    ' Specify that the following items are to be added to a bulleted list.
    11.    richTextBox1.SelectionBullet = True
    12.    ' Set the color of the item text.
    13.    richTextBox1.SelectionColor = Color.Red
    14.    ' Assign the text to the bulleted item.
    15.    richTextBox1.SelectedText = "Apples" + ControlChars.Cr
    16.    ' Apply same font since font settings do not carry to next line.
    17.    richTextBox1.SelectionFont = New Font("Arial", 12)
    18.    richTextBox1.SelectionColor = Color.Orange
    19.    richTextBox1.SelectedText = "Oranges" + ControlChars.Cr
    20.    richTextBox1.SelectionFont = New Font("Arial", 12)
    21.    richTextBox1.SelectionColor = Color.Purple
    22.    richTextBox1.SelectedText = "Grapes" + ControlChars.Cr
    23.    ' End the bulleted list.
    24.    richTextBox1.SelectionBullet = False
    25.    ' Specify the font size and string for text displayed below bulleted list.
    26.    richTextBox1.SelectionFont = New Font("Arial", 16)
    27.    richTextBox1.SelectedText = "Bulleted Text Complete!"
    28. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width