|
-
May 30th, 2000, 06:34 AM
#1
Thread Starter
Hyperactive Member
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
-
May 30th, 2000, 06:48 AM
#2
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
-
May 30th, 2000, 06:57 AM
#3
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
-
May 30th, 2000, 08:06 AM
#4
Thread Starter
Hyperactive Member
Hi Megatron,
I am going to give this a try. Hopefully it works.
Thanks,
Sal
-
May 30th, 2000, 08:19 AM
#5
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.
-
May 30th, 2000, 08:24 AM
#6
Thread Starter
Hyperactive Member
Working on it now.
Thanks again, will let you know results soon.
Sal
-
May 30th, 2000, 08:29 AM
#7
Thread Starter
Hyperactive Member
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?? - ??
-
May 30th, 2000, 08:37 AM
#8
Thread Starter
Hyperactive Member
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 ( ??? )
-
May 30th, 2000, 08:48 AM
#9
Fanatic Member
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!)
-
May 30th, 2000, 09:24 AM
#10
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?
-
May 30th, 2000, 10:50 PM
#11
Thread Starter
Hyperactive Member
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
-
May 30th, 2000, 11:00 PM
#12
Thread Starter
Hyperactive Member
...updated signature for VB version.
-
May 31st, 2000, 02:34 AM
#13
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)
-
May 31st, 2000, 04:16 AM
#14
Yup, i did all that. I think my other sub is conflicting with it somehow.
-
May 31st, 2000, 04:56 AM
#15
-
May 31st, 2000, 05:40 AM
#16
It doesnt give an error, it just doesnt change any of the keywords colors colors.
-
May 31st, 2000, 05:45 AM
#17
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. 
-
May 31st, 2000, 10:55 AM
#18
Thread Starter
Hyperactive Member
Hi,
Megatron, I'm going to change that vbBlue statement to iColour and check it out.
Sal
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|