Results 1 to 2 of 2

Thread: RichTextBox - Highlight / Bold all occurances of a word

  1. #1

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    RichTextBox - Highlight / Bold all occurances of a word

    VB Code:
    1. Private Sub ColorWord(prtbName As RichTextBox, _
    2.             pstrWord As String, _
    3.             plngHighlightColor As Long, _
    4.             Optional pblnBold As Boolean)
    5.            
    6. Dim lngPos As Long
    7. Dim lngSelStart As Long
    8. Dim lngSelLength As Long
    9.  
    10. lngSelStart = prtbName.SelStart
    11. lngSelLength = prtbName.SelLength
    12.  
    13. lngPos = prtbName.Find(pstrWord, 0)
    14.  
    15. Do
    16.      
    17.     prtbName.SelColor = plngHighlightColor
    18.    
    19.     If pblnBold = True Then
    20.         prtbName.SelBold = True
    21.     End If
    22.    
    23.     lngPos = prtbName.Find(pstrWord, lngPos + prtbName.SelLength)
    24.  
    25. Loop While lngPos > 0
    26.  
    27. prtbName.SelStart = lngSelStart
    28. prtbName.SelLength = lngSelLength
    29.  
    30. End Sub
    usage

    ColorWord RichTextBoxControl , "Word to Find", color ,[Optional BOLD as Boolean]
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: RichTextBox - Highlight / Bold all occurances of a word

    For a bit more flexibility when using the routine, add a checkbox to the form (call it chkBold) and a CommonDialog control. Then, use this to call the word coloring.
    VB Code:
    1. Private Sub Command1_Click()
    2. CommonDialog1.ShowColor
    3. If chkBold.Value = vbChecked Then
    4.    ColorWord RichTextBox1, Text1.Text, CommonDialog1.Color, True
    5. Else
    6.    ColorWord RichTextBox1, Text1.Text, CommonDialog1.Color
    7. End If
    8. End Sub
    This permits you to choose the color you wish to use when highlighting the word and to decide whether or not you want the word to be bolded after highlighting or not.

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