Results 1 to 2 of 2
  1. #1
    New Lounger
    Join Date
    Aug 2002
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    HIghlighting text in RichTextBox control (VB 6.0)

    Hello All,
    I have a RichTextbox with contants in them .........I need to highlight only certain words that are there in the RTB........Just like highlighting certain word in MS-Word. ............Is thhis possible?????Programatically.........
    If so please let me know how............ any help will be appreciated............

    Reets

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: HIghlighting text in RichTextBox control (VB 6.0)

    Reets, I had this all figured out and wrote the following reply:
    "You use the SelStart property and the SelLength property to highlight text, just the same as a textbox. There is not a Words collection as in MS Word, but you can use the Find method to find a word. The Find method finds, selects, and returns the position of a string or word. After you draw the rich textbox, press F1 and look and the properties and methods."

    But then I wrote a sample program and none of my advice worked! I discovered two keys: (1) reset SelStart and SelLength to 0 before doing a Find, (2) Be sure to SetFocus to the Rich Textbox.

    To look at the demo, Place a Rich Textbox and three command buttons on a blank form, double-click on a command button, and replace the code with the following. HTH --Sam

    <pre>Option Explicit

    Private Sub Command1_Click()
    With RichTextBox1
    .Text = "This is fun!?"
    .SelStart = 4
    .SelLength = 2
    .SetFocus
    End With
    End Sub

    Private Sub Command2_Click()
    With RichTextBox1
    .SetFocus
    .SelStart = 0
    .SelLength = 0
    .Find "is"
    End With
    End Sub

    Private Sub Command3_Click()
    With RichTextBox1
    .SetFocus
    .SelStart = 0
    .SelLength = 0
    .Find "is", , , rtfWholeWord
    End With
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

Posting Permissions

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