CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jun 2015
    Posts
    8

    Highlight string in RTF

    I know this has been done before, and I have code, that works somewhat, however it's not quite correct...
    My problem is the length of the string will vary, and i'm having trouble getting the length each time I try to find the string, here's my code...

    I need to highlight the string between "(Part", and "G53", but as I stated above the length will vary...
    I have placed a sample of the text to search below..
    Thanks for any help..

    Code:
    HighlightWords MAIN_WIRE_SHAPE.PROG_LIST, "(PART", "G53", vbYellow
    Code:
    Private Function HighlightWords(rtb As RichTextBox, sFindString As String, eFindString As String, lColor As Long) As Integer
    
            Dim lFoundPos As Long           'Position of first character
            Dim eFoundPos As Long           'of match
            
            Dim lFindLength As Long         'Length of string to find
            Dim lOriginalSelStart As Long
            Dim lOriginalSelLength As Long
            Dim iMatchCount As Integer      'Number of matches
    
            'Save the insertion points current location and length
            lOriginalSelStart = rtb.SelStart
            lOriginalSelLength = rtb.SelLength
    
            'Cache the length of the string to find
            'lFindLength = Len(sFindString)
    
            'Attempt to find the first match
            lFoundPos = rtb.Find(sFindString, 0, , rtfNoHighlight)
            eFoundPos = rtb.Find(eFindString, 0, , rtfNoHighlight)
            
            'Cache the length of the string to find
            lFindLength = eFoundPos - 4
            
            While lFoundPos > 0
              iMatchCount = iMatchCount + 1
    
              rtb.SelStart = lFoundPos - 1
              'The SelLength property is set to 0 as
              'soon as you change SelStart
              rtb.SelLength = lFindLength
              rtb.SelColor = lColor
              rtb.SelBold = True
              rtb.SelFontSize = 11
    
              'Attempt to find the next match
              
              
              lFoundPos = rtb.Find(sFindString, _
                lFoundPos + lFindLength, , rtfNoHighlight)
                
            Wend
    
            'Restore the insertion point to its original
            'location and length
            rtb.SelStart = lOriginalSelStart
            rtb.SelLength = lOriginalSelLength
    
            'Return the number of matches
            HighlightWords = iMatchCount
    
          End Function
    N1
    (PART NUMBER 1)
    (HEX DIA.= 1.00000)
    (W= 0.30000)
    [/COLOR]
    G53 G92X0.0000 Y0.0000
    G90 G54.1 P0 G00 X0.00000 Y-0.35355 M91
    Z[H117+.00000]
    (MITSUBISHI - MITSUBISHI (MULTI-PART - MULTIPLE JOBS ENABLED)
    H01 = 0.0075
    G90
    G92X0.0Y0.0
    M20
    M78
    M78
    M80 M82 M84
    E1231F.12
    M90
    M102
    H01
    G42G01X-0.12695Y-0.01978
    G02X-0.15000Y0.0I-0.00304J0.01978
    M106Q0
    G01Y0.08661
    G01X0.0Y0.17321
    G01X0.15000Y0.08661
    G01Y-0.08661
    G01X0.0Y-0.17321
    G01X-0.15000Y-0.08661
    G01Y-0.05
    M00
    G01Y0.0
    G02X-0.12695Y0.01978I0.02001J0.0
    G40G01X0.0Y0.0
    M85 M83 M81
    M21
    M58
    G90 Z[H117+0.25] M91 F15.0
    N2
    (PART NUMBER 2)
    (HEX DIA.= 1.00000)
    (W= 0.30000)

    G53 G92X0.0000 Y0.0000
    G90 G54.1 P1 G00 X0.00000 Y-0.35355 M91
    Z[H117+.00000]
    (MITSUBISHI - MITSUBISHI (MULTI-PART - MULTIPLE JOBS ENABLED)
    H01 = 0.0075
    G90
    G92X0.0Y0.0
    M20
    M78
    M78
    M80 M82 M84
    E1231F.12
    M90
    M102
    H01
    G42G01X-0.12695Y-0.01978
    G02X-0.15000Y0.0I-0.00304J0.01978
    M106Q0
    G01Y0.08661
    G01X0.0Y0.17321
    G01X0.15000Y0.08661
    G01Y-0.08661
    G01X0.0Y-0.17321
    G01X-0.15000Y-0.08661
    G01Y-0.05
    M00
    G01Y0.0
    G02X-0.12695Y0.01978I0.02001J0.0
    G40G01X0.0Y0.0
    M85 M83 M81
    M21
    M58
    G90 Z[H117+0.25] M91 F15.0
    N3
    (PART NUMBER 3)
    (OBLONG DIA.= 1.00000)
    (P= 0.60000 W= 0.30000)

    G53 G92X0.0000 Y0.0000
    G90 G54.1 P2 G00 X0.00000 Y-0.35355 M91
    Z[H117+.00000]
    (MITSUBISHI - MITSUBISHI (MULTI-PART - MULTIPLE JOBS ENABLED)
    H01 = 0.0075
    G90
    G92X0.0Y0.0
    M20
    M78
    M78
    M80 M82 M84
    E1231F.12
    M90
    M102
    H01
    G42G01X-0.12695Y-0.01978
    G02X-0.15000Y0.0I-0.00304J0.01978
    M106Q0
    G01Y0.15000
    G02X0.15000I0.15000J0.0
    G01Y-0.15000
    G02X-0.15000I-0.15000J0.0
    G01Y-0.05
    M00
    G01Y0.0
    G02X-0.12695Y0.01978I0.02001J0.0
    G40G01X0.0Y0.0
    M85 M83 M81
    M21
    M58
    G90 Z[H117+0.25] M91 F15.0
    N4
    (PART NUMBER 4)
    (OBLONG DIA.= 1.00000)
    (P= 0.60000 W= 0.30000)

    G53 G92X0.0000 Y0.0000
    G90 G54.1 P3 G00 X0.00000 Y-0.35355 M91
    Z[H117+.00000]
    (MITSUBISHI - MITSUBISHI (MULTI-PART - MULTIPLE JOBS ENABLED)
    H01 = 0.0075
    G90
    G92X0.0Y0.0
    M20
    M78
    M78
    M80 M82 M84
    E1231F.12
    M90
    M102
    H01
    G42G01X-0.12695Y-0.01978
    G02X-0.15000Y0.0I-0.00304J0.01978
    M106Q0
    G01Y0.15000
    G02X0.15000I0.15000J0.0
    G01Y-0.15000
    G02X-0.15000I-0.15000J0.0
    G01Y-0.05
    M00
    G01Y0.0
    G02X-0.12695Y0.01978I0.02001J0.0
    G40G01X0.0Y0.0
    M85 M83 M81
    M21
    M58
    G90 Z[H117+0.25] M91 F15.0M00
    G28 X0.0000 Y0.0000 Z0.0000 M91 F50.
    M30
    Last edited by supercrewed; February 2nd, 2016 at 08:23 AM.

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

Featured