Results 1 to 18 of 18

Thread: RichTextBox1.Find (...want to find more than one instance)

  1. #1

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    I am using a rich textbox. Inside that box I am searching for a particular word. Let's say the word is "up". The sentence is: "Up up and away-ay, in my beautiful balloon."

    Here there are 2 instances of up. How do you find both(or all if there were 5 instances of up) using the RichTextBox1.find ("up") statement/code???

    My reason for finding this is to color all instances of the find result a certain color. Say red for example. So...up would appear red twice in my rich textbox. This is similiar to how you search help. You know, it gives you every instance of the word that you searched for in green. That's what I'm trying to do.

    Any ideas???

    Sal

  2. #2
    Guest
    I have an example of this. I originall used this coding for an HTML Editor and a BASIC Compiler, to highlight Keywords and Tags, but I think it might also work for you.

    Make a Form with a RichTextBox on it.

    Put this code in a module

    Code:
    Private Sub HighlightText(sKeyword As String)
    
    Dim nStart As Integer, sPrevChar As String, sNextChar As String
    
                                               
    nStart = InStr(1, LCase(RichTextBox1.Text), sKeyword)
                                                
                                                
    Do While nStart <> 0
    
    If nStart > 1 Then
        sPrevChar = Mid$(RichTextBox1.Text, nStart - 1, 1)
    Else
        sPrevChar = " "
    End If
                                                    
    
    If Len(RichTextBox1.Text) >= nStart + Len(sKeyword) Then
        sNextChar = Mid$(RichTextBox1.Text, nStart + Len(sKeyword), 1)
    Else
        sNextChar = " "
    End If
                                                       
    
    If (sPrevChar = Chr(32) Or sPrevChar = Chr(13) Or _
    sPrevChar = Chr(10) Or sPrevChar = Chr(9)) And _
    (sNextChar = Chr(32) Or sNextChar = Chr(13) Or _
    sNextChar = Chr(10) Or sNextChar = Chr(9)) Then
    
    With RichTextBox1
    
    .SelStart = nStart - 1
    .SelLength = Len(sKeyword)
    .SelColor = vbBlue          ' Change this to whatever colour you want
    .SelText = UCase(sKeyword)
    .SelStart = Len(RichTextBox1.Text)
    .SelColor = vbBlack
    
    End With
    
    End If
                                                  
    nStart = InStr(nStart + Len(sKeyword), LCase(RichTextBox1.Text), sKeyword)
    
    Loop
    
    End Sub
    If you would like to have the words change colour as you type than put this code in the RichTextBox's Change event. If you would like a CommandButton to be clicked, than simpily put that code in a CommandButton's Click Event.

    Code:
    Private Sub RichTextBox1_Change()
    
    With RichTextBox1
    .SelStart = 0
    .SelLength = Len(.Text)
    .SelColor = vbBlack
    .SelStart = Len(.Text)
    End With
    
                                        
    ' Add more custom words here
    HighlightText "if"
    HighlightText "else"
    HighlightText "then"
    HighlightText "print"
    HighlightText "input"
    HighlightText "open"
                             
     
    End Sub

  3. #3
    Guest
    I have just worked on a new Function for you. I modified the above code so that you can choose what colour you want.

    First you have to change some code. Replace this line Private Sub HighlightText(sKeyword As String) with this new line.

    Code:
    ' The iColour is added for custom colour
    Private Sub HighlightText(sKeyword As String, iColour As Long)
    Now find this line .SelColor = iColour

    And Replace it with this line
    Code:
    ' change the fixed value to our custom colour variable
    .SelColor = iColour
    Now, to use it use this
    Code:
    HighlightText "blue", vbBlue

  4. #4

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    Hi Megatron,

    I am going to give this a try. Hopefully it works.

    Thanks,
    Sal

  5. #5
    Guest
    It doesn't really use the Find method as you specified, but it still Highlight's the Text. Make sure to change the lines as I specified in my second post. That way, you can choose what colour you want each word to be.

  6. #6

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    Working on it now.

    Thanks again, will let you know results soon.
    Sal

  7. #7

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    Megatron,

    You said:

    Now find this line .SelColor = iColour
    And Replace it with this line
    Code:
    ' change the fixed value to our custom colour variable
    .SelColor = iColour
    There seems to be no change there????

    Sal?? - ??


  8. #8

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262

    Thumbs up

    Megatron,

    It works! You are a genious. I am really impressed with your knowledge. I will spend the next hour studying your code and trying to figure out what makes it work. I have not yet mastered the (code within the paren) usage yet -->

    Code:
    nStart = InStr(1, LCase(RichTextBox1.Text), sKeyword)
    Thanks again,
    Sal

    P.S. - Only if you have time... Maybe you can diasect that code for me, especially the stuff inside the ( ??? )

  9. #9
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    In loops like this dump the richtext.text to a string, then use the instr() on the string.

    Call the rtf control as little as possible.

    The performance boost is worth the effort of changing the code.

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  10. #10
    Guest
    Hey Megatron,
    I cant seem to get it to work in my program. I cut and pasted and then changed the names appropiatly. Any ideas?

  11. #11

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    There must be a simpler way to do this using the .find method. I have studied Megatron's code and am getting somewhat of an understanding of it, but there are still some statements that are unfamiliar to me, such as Lcase (maybe that means "Lower Case"...need a syntax reference).

    Additionally, during execution, all letters in every word, eventually turn blue (not just the search word), which obviously is undesirable. It seems like there should be some code which would find all instances of the search word using the .find method. Like .find(all)

    Any other approaches/ideas?

    Sal

  12. #12

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    ...updated signature for VB version.
    VB-6 Professional Edition (Sp3)
    www.safemall.cc
    [email protected]

    Climb & Maintain FL410,
    Cleared Direct Rainr

  13. #13
    Guest
    Yes, the Lcase means Lower Case.

    ChimpFace9000. It should be able to work. Did you make sure to erase the old lines then replace with the new lines? Also, the syntax to call the function is different. Instead of using HighlightText "blue", you would use HighlightText "blue", vbBlue. (Make sure to add the colour)

  14. #14
    Guest
    Yup, i did all that. I think my other sub is conflicting with it somehow.

  15. #15
    Guest
    What does the error say?

  16. #16
    Guest
    It doesnt give an error, it just doesnt change any of the keywords colors colors.

  17. #17
    Guest
    Now I know why!! I exaplaind it wrong. I mant find the line .SelColor = vbBlue. Now replace the vbBlue with iColour

    This should clear things up.

  18. #18

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    Hi,

    Megatron, I'm going to change that vbBlue statement to iColour and check it out.

    Sal
    VB-6 Professional Edition (Sp3)
    www.safemall.cc
    [email protected]

    Climb & Maintain FL410,
    Cleared Direct Rainr

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