-
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
-
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
-
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
-
Hi Megatron,
I am going to give this a try. Hopefully it works.
Thanks,
Sal
-
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.
-
Working on it now.
Thanks again, will let you know results soon.
Sal
-
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?? - ??
-
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 ( ??? )
-
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.
-
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?
-
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
-
...updated signature for VB version.
-
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)
-
Yup, i did all that. I think my other sub is conflicting with it somehow.
-
-
It doesnt give an error, it just doesnt change any of the keywords colors colors.
-
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. :)
-
Hi,
Megatron, I'm going to change that vbBlue statement to iColour and check it out.
Sal